Logging

The logging module of Redis Cache Pro allows you to log and analyze anything your WordPress app does, as well as easily debug issues.

By default, Redis Cache Pro uses the NullLogger that does, you guessed it, nothing.

When WP_DEBUG or SAVEQUERIES is enabled, it defaults to the ArrayLogger, which stores all log entries on a per-request basis, which is used for the Debug Bar integration to show you who is calling the cache, how long it takes, and what’s being done.

You can, of course, use your own logging implementation, any PSR-3 compatible logger, or one of the included loggers such as the ErrorLogLogger.

Custom loggers for AWS CloudWatch, Coralogix, Papertrail, and others will be added upon request.

Loggers

NullLogger

It does nothing to keep things memory efficient.

ArrayLogger

Stores all log entries in memory and can be read using ArrayLogger::messages().

ErrorLogLogger

Extends to ArrayLogger and additionally it sends all log messages to stderr using PHP error_log() function.

To restrict what message levels are logged (for example, only errors), you can use the log_level configuration option:

wp-config.php
define('WP_REDIS_CONFIG', [
'logger' => \RedisCachePro\Loggers\ErrorLogLogger::class,
'log_levels' => ['emergency', 'alert', 'critical', 'error', 'warning'],
]);

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:

wp-config.php
define('WP_REDIS_CONFIG', [
// ...
'logger' => \RedisCachePro\Loggers\ErrorLogLogger::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, which implements all necessary methods except the log() method itself:

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.");
}
syslog($level, (string) $message);
closelog();
}
}