Logging
The logging module of Object Cache Pro allows you to log and analyze anything your WordPress object cache does, as well as easily debug issues.
By default, Object Cache Pro uses the ErrorLogLogger
which logs all critical errors to stderr
using PHP’s error_log()
function.
When WP_DEBUG
or SAVEQUERIES
is enabled, the ArrayLogger
is used, which stores all log entries on a per-request basis.
You can, of course, use your own logging implementation, any PSR-3 compatible logger, or one of the included loggers such as the NullLogger
.
Loggers
NullLogger
It does nothing to keep the memory footprint low.
ArrayLogger
Stores all log entries in runtime memory and can be read using ArrayLogger::messages()
.
Additionally extends the ErrorLogLogger
class, which logs critical errors to strerr
.
ErrorLogLogger
All critical errors are logged to stderr
using PHP error_log()
function.
You can adjust the log_levels
configuration option to adjust which messages are logged.
define('WP_REDIS_CONFIG', [
'logger' => \RedisCachePro\Loggers\ErrorLogLogger::class,
// log all messages
'log_levels' => null,
// only log warnings and notices
'log_levels' => ['warning', 'notice'],
]);
BacktraceLogger
For quick debugging, the BacktraceLogger
will ignore the log_levels
configuration option and send all message to stderr
using PHP error_log()
function.
define('WP_REDIS_CONFIG', [
'logger' => \RedisCachePro\Loggers\BacktraceLogger::class,
'save_commands' => true,
]);
Debug logging
Sometimes you may want to quickly debug something without creating your own logger class and just pass a function name as the logger
configuration option.
define('WP_REDIS_CONFIG', [
'logger' => 'my_debug_logger',
'log_levels' => null, // log all messages
]);
function my_debug_logger($level, $message, array $context = [])
{
error_log("objectcache.{$level}: {$message}");
}
Custom Loggers
Loggers must implement the RedisCachePro\Loggers\LoggerInterface
which is identical to PSR-3’s Psr\Log\LoggerInterface
.
To register a logger, pass its fully qualified class name to your config:
require_once __DIR__ . '/path/to/SyslogLogger.php'
define('WP_REDIS_CONFIG', [
'logger' => SyslogLogger::class,
]);
An instance of it will be created when WordPress calls wp_cache_init()
.
To keep things simple when writing your own logger, you can extend RedisCachePro\Loggers\Logger
class, which implements all necessary methods except the log()
method itself:
<?php
require_once __DIR__ . '/wp-content/plugins/object-cache-pro/bootstrap.php';
class SyslogLogger extends RedisCachePro\Loggers\Logger
{
/**
* Logs with an arbitrary level to the system log.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = [])
{
if (! openlog('wordpress.redis', LOG_PID, LOG_USER)) {
throw new \LogicException("Can’t open syslog for ident LOG_PID and facility LOG_USER.");
}
$priority = str_replace(
['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'],
[LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG],
$level
);
syslog($level, (string) $message);
closelog();
}
}