Logger

WordPress doesn’t have a propper logger function, so we created one. The logger is available through Cuisine’s utility system by using the following class:

use Cuisine\Utilties\Logger


Logging

Obviously the logging class is there to log messages. There are two kinds of messages in cuisine: errors and regular messages.

Logging errors

Logging errors if fairly simple:

Logger::error( 'Ohw no, something went wrong!' ); 

Logging errors will always come with a stack-trace of functions previously called. How big this stack trace is depends on the log level you set:

Setting the log level

The log level is detirment by a constant:CUISINE_LOGLEVEL The possible values of this are represented in a Cuisine\Utilities\LogLevel class. The LogLevel states are represented by integers. Here’s an example:

use Cuisine\Utilities\LogLevel;
echo LogLevel::FUNCTIONNAMES;// returns 1. Only show involved function names
echo LogLevel::CLASSNAMES;// returns 2. Also show class names
echo LogLevel::FULL;// returns 3. Displays the full stack trace.

The default LogLevel is 1 (function names).


Logging messages

Logger::message( 'Here\'s a message from the logger class' );

Before messages show up in our actual log we need to set a constant, which isn’t defined by default. Set the CUISINE_LOG_MESSAGES constant to true to make sure messages get logged.


The log file

The default log file that will get written when using the Logger is

PATH_TO_SITE/wp_content/logs/cuisine.log

If you wish to change that you can use two filters:

Changing the path

Changing the logger path can be done as followed:

add_filter( 'cuisine_log_path', function( $path ){
    return WP_CONTENT_DIR . DIRECTORY_SEPERATOR . 'uploads' . DIRECTORY_SEPERATOR . 'logger'
});

Note that this path doesn’t need a directory seperator at the end. Important notice: please make sure this directory is writable by php.


Changing the filename

Changing the logger filename can be done like this:

add_filter( 'cuisine_log_filename', function( $file ){
    return date('Y-m-d').'-'.$file; //add a date in front of your log.
});

In the example above the $file variable already has the ‘.log’ extension added, but if you where to completely replace that, please note that you need to add the extension manually.


Displaying messages and errors on the screen.

Both the Logger::error() and Logger::message() functions come with an aditional parameter. This will render the error or message on the screen (which can be handy for debugging on a local machine).

Here’s an example:

Logger::error( 'Something went wrong', true );

The second parameter ensures this error will be displayed by WordPress as wel as logged in our log-file. It’s default value is false however.

Globally controlling wether or not errors & messages are displayed

Via another constant we can also control if all errors and messages will be displayed by WordPress as well as logged. Setting CUISINE_DISPLAY_LOG to true will ensure ALL errors and messages are displayed:

define('CUISINE_DISPLAY_LOG', true );