Logger
The Logger utility class provides a set of methods to log messages to one or more log files. These log files are stored in the wp-content/slug-logs directory. Where slug is the slug of your plugin.
Setting up the logs directory
To set up the logs directory, use the initialize_logs_directory() static method from the FileSystem utility class. This method will create the directory if it doesn't exist, and will set the appropriate permissions.
For more information about the FileSystem utility class, refer to the filesystem documentation.
Creating an instance of the logger
To create an instance of the logger, initialize the Logger class with the Plugin instance as a parameter.
use Sematico\Metapress\Utils\Logger;
$logger = new Logger( $plugin );The constructor of the Logger class accepts the Plugin instance as a parameter. Additional parameters can be passed to the constructor to configure the logger.
Log level threshold
By default, the logger will log messages with a log level of DEBUG and higher. To change the log level threshold, pass the desired log level as the second parameter to the constructor.
use Sematico\Metapress\Utils\Logger;
$logger = new Logger( $plugin, LogLevel::INFO );The available log levels are:
protected $log_levels = [
LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1,
LogLevel::CRITICAL => 2,
LogLevel::ERROR => 3,
LogLevel::WARNING => 4,
LogLevel::NOTICE => 5,
LogLevel::INFO => 6,
LogLevel::DEBUG => 7,
];These are inferred from the PSR-3 log levels.
Additional options
The logger constructor accepts an optional third parameter, which is an array of options. The available options are:
| Option | Default | Description |
|---|---|---|
| dateFormat | Y-m-d G:i:s.u | The format of the date that may be included in the log filename (php formatted) |
| extension | log | The log file extension |
| filename | [prefix][date].[extension] | Setting a custom filename overrides the prefix and extension options |
| flushFrequency | false (disabled) | How many lines to flush the output buffer after |
| prefix | 'log_' | The log file prefix |
| logFormat | false | Format of log entries |
| appendContext | true | When false, don't append context to log entries |
Log format
By default, the logger will log messages in the following format:
[date] [level] messageTo change the log format, pass the desired format as the third parameter to the constructor and set the logFormat option to your desired format.
The format must be formatted as a string that contains the following placeholders:
{date}- The date of the log entry{level}- The log level of the log entry{level-padding}- The whitespace padding for the log level and make all lines line uppriority- The priority of the log entry inferred from the log level{message}- The message of the log entry{context}- The context of the log entry
For example, to change the log format to the following:
{date} {level} {message}Pass the following options to the constructor:
use Sematico\Metapress\Utils\Logger;
$logger = new Logger( $plugin, LogLevel::INFO, [ 'logFormat' => '{date} {level} {message}' ] );Log file name
By default, the logger will log messages to a file named log_[date]_[md5hash].log. The [date] is inferred from the dateFormat option, and the [md5hash] is the md5 hash of the whole options array.
Logging messages
To log a message, use one or more of the available methods from the Logger class.
The available methods are:
emergency($message, $context = null)alert($message, $context = null)critical($message, $context = null)error($message, $context = null)warning($message, $context = null)notice($message, $context = null)info($message, $context = null)debug($message, $context = null)
The first parameter is the message to log, and the second parameter is the context of the log entry.
$logger->info('This is an info message');NOTE
The debug() method is the most verbose of the logging methods.