Skip to content

WordPress Logger: A plugin that displays log messages to the Safari and Firefox console from PHP

  • by

Essential debugging tool for plugin and theme developers.

Display log messages from PHP in the browser console in Safari and Firefox (with firebug). You no longer have to use print_r statements from PHP to figure out what is going on in the code. Besides, print_r statements  mess up the DOM and HTML layout. 

Features

  • Log debug messages directly from themes and plugins.
  • Display log messages in the browser console, without muddying up the browser display.
  • Displays complex structures such as arrays and objects in pretty print.
  • Shows the line number and file from where the message was logged ( you won’t lose track of log statements ).

Screenshots

Installation

  1. Verify that you have PHP5, which is required for this plugin.
  2. Download the whole wplogger folder into the /wp-content/plugins/directory.
  3. Activate the plugin through the Plugins menu in WordPress.

Requirements

  • Make sure that your theme template has a footer ( index.php should have a get_footer() function call at the end).
  • Turn on the console in your browser:
    • Firefox: The Firebug extension needs to be installed and activated.
    • Safari: Show the Error Console from the Debug/Develop menu.
      Details on how to enable the Debug menu for Safari on OSX and Windows.

Usage

After activating the plugin, the following PHP function call can log any PHP expression to the console log.

$wplogger->log( php_expression [, message_type] );

The message_type is optional and can be any one of the following constants:

  • WPLOG_ERR
  • WPLOG_WARNING
  • WPLOG_INFO
  • WPLOG_DEBUG

Use cases

Logging from template files

This is from inside the loop to display post IDs.

<?php $wplogger->log( 'Post ID: '.$post->ID ); ?>

Output:

[Information: from line 20 in file index.php] Post ID: 125
[Information: from line 20 in file index.php] Post ID: 116
[Information: from line 20 in file index.php] Post ID: 65

Logging from PHP files

This is from  functions.php ( always a good idea to check if $wplogger is available ). Note the message type set to warning through the second parameter.

if ($wplogger) $wplogger->log( get_option('active_plugins'), WPLOG_WARNING );

Output:

[Warning: from line 55 in file functions.<span>php</span>] array (
	  0 => 'wplogger/wplogger.php',
	  1 => '12seconds-widget/12seconds-widget.php',
	  2 => 'get-the-image/get-the-image.php',
)

Logging from plugins

This is from inside a plugin function. Note the global statement to get $wploggerinto current scope.

global $wplogger; $wplogger->log( 'No images attached to this post', WPLOG_ERR );

Output:

[Error: from line 206 in file get-the-image.php] No images attached to this post

Credits

Code that forces the wplogger plugin to load first was adapted from the WordPress FirePHP plugin developed by Ivan Weiller.

This plugin is based on PEAR Log, the logging framework that is part of the PHP PEAR library. Current maintainers Jon Parise, Jan Schneider, and Chuck Hagenbuch. PEAR Log is based on code first developed for the Horde 1.3 framework – original authors Chuck Hagenbuch, and Jon Parise.

Download Plugin

Feature requests and critiques

This is the first iteration of the wplogger plugin. Please comment on how you would like to see it improved.

Leave a Reply

Your email address will not be published. Required fields are marked *