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

wplogger_icon

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.

Download wplogger plugin

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

Firefox console showing debug messages.

Firefox console showing debug messages.


Safari console showing debug messages

Safari console showing debug messages

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 $wplogger into 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

Download wplogger 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.

  • Share/Bookmark

13 Comments

  1. RavanH says:

    What an excellent plugin! Any chance of this working with Opera Dragonfly soon too? Thank you for sharing :)

  2. Chandima Cumaranatunge says:

    @RavanH thanks for bringing Opera Dragonfly to my attention. It’s been quite a while since I’ve fired up an Opera Browser, but I’ll certainly look into integrating it. Glad you like the plugin.

  3. Chandima Cumaranatunge says:

    @RavanH I’ve added support for Opera. Log messages are now visible on the Opera Dragonfy error console in version 0.3 of the plugin. Please provide some feedback on performance as I didn’t have much time for comprehensive testing on Opera.

  4. RavanH says:

    It works! No issues whatsoever :)

    One question though: Right now it displays error logging to any visitor (visible either in source code or on error console) but would it be safer to log only to admin? And maybe make it optional (with a warning) to log other users?

  5. Chandima Cumaranatunge says:

    Hi RavanH, Thanks for checking it out. Logging only if admin was part of future functionality. I’ll implement it ASAP.

  6. tf0054 says:

    Cool plugin!
    I’m using wplogger on v2.8.4 and No error was occured.

  7. Chandima Cumaranatunge says:

    @tf0054 thanks for testing the plugin on the latest version of WP.

  8. Aleksandar says:

    Hi,

    You might want to register the `flushLogMessages` function with the `admin_footer` action hook, too. Otherwise it doesn’t work in the admin area for me:

    # MOD:: register admin footer, too:
    add_action( ‘admin_footer’, array($wplogger, ‘flushLogMessages’) ); // log scripts

    Other than that it works great :)

    Take care,
    Aleksandar

  9. Oleg says:

    Not working in plugin page.
    global $wplogger; $wplogger->log( ‘No images attached to this post’, WPLOG_ERR );
    At $wplogger information is present, but Firebug does not display debug information.

  10. Pitschi says:

    plugin not working if WP root directory is a subfolder eg http://exemple.com/WPress/

  11. Chandima Cumaranatunge says:

    @Pitschi I’m not sure why it doesn’t work with a WP subfolder install. The only directory dependency is a plugin_basename( __FILE__ ) call in the methods that re-order the plugin list. Anybody know if the plugin_basename function breaks on a subfolder install? Or could it be some other issue?

  12. RavanH says:

    plugin_basename( __FILE__ ) should not cause any problem on a subfolder install… been looking through the code a bit but cannot find anything that would explain this. no get_option(’site_url’ / ‘home’) mixup no hardcoded paths (as far as I could see)… weird.

    the only thing I noticed is wp_enqueue_script( ‘jquery’, ‘1.2,6′ ) , should that not be wp_enqueue_script( ‘jquery’, ‘1.2.6′ ) ?

  13. Daniel says:

    Unfortunatly it does not work when page redirection is involved :-(

Leave a Comment