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 string message:

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

An instance of WP_ErrorException, or Throwable 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 and they’ll be formatted appropriately:

$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' );

The QM class is PSR-3 compatible, although it doesn’t actually implement Psr\Log\LoggerInterface.

Does Query Monitor help you debug your site without pulling your hair out?
If so, please consider sponsoring my work on Query Monitor ❤️. Thanks!