Debugging PHP With Firebug

February 8th, 2007

Logging PHP and JavaScript errors to same window makes debugging easier. It also might be the cure to cancer and bring world peace. In general…

In a recent PHP + JavaScript project I realized how annoying it is to have debug information in more than one place. For PHP I use PEAR::Log package. Log is written to file. For JavaScript I use Firebug. Log is written to Firebug console. When something goes wrong you must switch between browser and terminal windows.

There must be a better way. I like both PEAR::Log and Firebug. Obvious thing was to combine them. Result is Firebug handler for PEAR::Log. It can currently be found only in CVS. Hopefully be handler will be included in next release of PEAR:Log package.

Example PHP usage below.

 $log = &Log::singleton('firebug', '', 'PHP',
                        array('buffering' => true),
                        PEAR_LOG_DEBUG); 
 $log->log('Debug lorem ipsum.',             PEAR_LOG_DEBUG);
 $log->log('Info wisi enim ad minim veniam', PEAR_LOG_INFO);
 $log->log('Warning est usus legentis in',   PEAR_LOG_WARNING);
 $log->log('Error est notare quam',          PEAR_LOG_ERR);

If you have Firebug installed, enable it and check Log Firebug test page. You can also redirect all PHP errors to Firebug console.

UPDATE 20070208: You might want to check FirePHP which provides different approach. FirePHP is Firefox extension built on top of Firebug. It modifies request headers to include Accept: text/firephp. Debug data is sent back using text/firephp section of multipart/mixed HTTP response. Using FirePHP is more complicated but at the same time it can provide you with more flexibility.


13 Responses to “Debugging PHP With Firebug”

  1. till says:

    I followed you here from pear-dev, and I must say that this is pretty hot. :)

  2. Mika Tuupola says:

    Thanks! Idea came from need. I am glad others have found it usefull too.

  3. Christoph Dorn says:

    The following article provides a solution that allows you to log from PHP to the Firebug Console without the requirement for PEAR::Log. (It also includes JavaScript code in the response though which is what FirePHP eliminates):

    http://ajax.phpmagazine.net/2007/02/how_to_use_firebug_to_debug_ph.html

  4. Mika Tuupola says:

    I saw a link to Buggy in jquery-discuss. Looks interesting. For me requiring PEAR::Log is not a burden though. Most of my code already uses it for logging.

    Normally I use composite handler which consists of file and mail handlers. All log data goes to file. Anything critical such as failing database connection gets sent to mail handler. If I want to log also to Firebug console, all it takes is to add firebug handler into composite handler. No need to do other code changes.

    In the end it is matter of taste. If already using PEAR::Log it makes sense to choose PEAR::Log and firebug handler over Buggy.

    That said, PEAR::Log is just abstracted logging system with unified api to different log storages. FirePHP in other hand is better debugging tool. I am looking forward to use FirePHP for next larger project in the near future. It is a great companion to Firebug!

  5. German Rumm says:

    Hi,

    First of all, thanks for jquery-editable plugin (came here for it)

    Some code suggestion:

    you don’t need to use global $log; in your errorHandler, you can just use $log = Log::singleton(‘composite’);

  6. Mika Tuupola says:

    Thanks. Changed it in example. Using globals was horrible style mistake…

    You seem to be located in Estonia too?

  7. German Rumm says:

    Yes, in Tallinn, actually :-)

  8. Brock Harris says:

    In your http://www.appelsiini.net/~tuupola/test/php_error_to_firebug.php page, you have a minor typo on your default: priority case statement. You have ”$priotity” instead of ”$priority”.

    Super cool concept! My co-workers were bedazzled.

  9. Mika Tuupola says:

    Fixed. Thanks for the heads up. Actually did not ever remember I still have some stuff under the /~tuupola/ url…

  10. Christoph Dorn says:

    There have been lots of changes to FirePHP including the addition of being able to easily log to the Firebug console without affecting the content on your page.

    Take another look at the homepage http://www.firephp.org/.

    Thanks for the interest and support!

    Christoph

  11. Mika Tuupola says:

    Thanks for the heads up. It has been a while since I last looked at FirePHP. New features look really promising.

    Keep up the good work!

  12. tow says:

    For those who are on mac, what about a real PHP debugger: macGDBP is out! :)

    http://www.bluestatic.org/software/macgdbp/

  13. Mika Tuupola says:

    tow: Looks interesting. Thanks for notifying :)

Sorry, comments are closed for this article.