Logging Variables

Messages and variables can be logged in Query Monitor, similarly to how you can call console.log in JavaScript to log data to the console. This can be used as a replacement for var_dump().

do_action( 'qm/debug', 'This happened!' );

You can use any of the following actions which correspond to PSR-3 and syslog log levels:

  • qm/emergency
  • qm/alert
  • qm/critical
  • qm/error
  • qm/warning
  • qm/notice
  • qm/info
  • qm/debug

A log level of warning or higher will trigger a notification in Query Monitor’s admin toolbar.

Here’s what the Logs panel looks like when some messages have been logged:

Query Monitor's Logging Panel

The curly brace syntax can be used to interpolate values into a message:

do_action( 'qm/warning', 'Unexpected value of {foo} encountered', [
    'foo' => $foo,
] );

WP_Error or Exception object can be passed directly into the logger:

if ( is_wp_error( $response ) ) {
    do_action( 'qm/error', $response );
}
try {
    // your code
} catch ( Exception $e ) {
    do_action( 'qm/error', $e );
}

Variables of any type can be logged:

$var = [ 1, 2, 3 ];
do_action( 'qm/debug', $var );

Finally, the static methods on the QM class can be used instead of calling do_action(), but you shouldn’t normally need to do this:

QM::error( 'Everything is broken' );