Skip to content

Wonolog

Spencer Mandzik edited this page Apr 27, 2018 · 9 revisions

Wonolog is a monolog-based logging package for WordPress. Not to be used for the live site.

Configuration

  • Run composer update if package is not installed.
  • Ensure Composer autoload is loaded in wp-config.php or anytime before the 'muplugins_loaded' action is fired:
$autoload_path = "/path/to/vendor/autoload.php";
if (file_exists($autoload_path)) { require $autoload_path; }
  • MU plugin that, at least, contains this code:
    if ( defined( 'Inpsyde\Wonolog\LOG' ) ) { Inpsyde\Wonolog\bootstrap(); }

In the Chappress repo, an mu-plugin was created to enable and bootstrap Wonolog.
/wp-content/bootstrap-wonolog.php

Logging

The three steps described above are all that is necessary to have a working logging system that uses Monolog to write logs in a file.

What is actually logged depends on the value of WP_DEBUG_LOG constant. When WP_DEBUG_LOG is set to true, Wonolog will log everything.

Custom logging in Wonolog is done via a WordPress function: do_action()
The main hook to use for the scope is wonolog.log.
Docs

Example:
do_action( 'wonolog.log', 'Some event happened in ' . $_SERVER['SCRIPT_NAME'] );

A log can be customized with more information.The most important ones being the channel and the level of the log event.

do_action( 'wonolog.log', [
    'message' => 'Something happened.',
    'channel' => 'DEBUG',
    'level'   => 100,
    'context' => [],
] );

Log Path

The default log path is set to /wp-content/wonolog. The /wp-content directory needs to be writeable by the server.

To change the log path, set this in the mu-plugin:
putenv( 'WONOLOG_DEFAULT_HANDLER_ROOT_DIR=/var/log/wonolog' );

Additional Notes

In the wp-config.php file, using a log while running the WP CLI will display an error:

PHP Fatal error:  Uncaught Error: Call to undefined function do_action() in the wp-config file 

The wp-config.php is calling a WordPress function before WordPress has been loaded to define it. PHP will fail out with a fatal error. The log will work but the WP CLI error will persist.

A temporary workaround to run the wp cli AND log in wp-config.php:

if ( ! defined( 'WP_CLI' ) && ! WP_CLI) {
  do_action( 'wonolog.log', 'Some event happened in wp-config.php' );
}

Resources

Must-Use Functions
Bootstrapping
Php Error

Clone this wiki locally