Debugging with FirePHP

Code Naked

  • Homepage
  • About
  • Contact

Sep 12: Debugging with FirePHP

One of the nicest features to have when you are developing an application is a decent debug console so that you can see what your application is doing without having to look through ten log files and put var_dumps that you have to comment out all through your code. In the Zend Framework v1.6 we get a new feature based on FirePHP which will allow us to log directly to the Firebug console with a simple API call from PHP. Here is an example of how this looks in my application:

It's actually pretty easy to get this working, I did have to make one change to the Wildfire library though when I first started using it in order to get it behaving as I expected. I opened a bug which was resolved before 1.6 rolled out the door. In setting this up, I wanted the debugging to be as transparent as possible, one of the great things about Zend_Log is the ability to add filters to the individual writers so that the main log writer that writes things out to a file can ignore log messages with a priority of debug or info quite easily. This means that on production we can just not include the Firebug writer and we can rest assured that no sensitive information is being sent to the browser.

Okay, lets break down what I have done:

1. I changed the Error Controller so that it logs exceptions
2. I added a profiler to my Db class
3. I added a custom Memcached class so that I could ask it for stats
4. I modified my application environment so that it could know if was in production or development
5. I have the Workspace logging init add the Firebug logger if we are in dev
6. I added the Debug Front Controller plugin so that I could log stuff like action exec times etc
7. I installed the FirePHP extension for Firefox
8. I enabled console and net for my site in Firebug
9. I saw glorious debug info and went to get a beer to celebrate.

Although it's great for developers to have this look into your applications inner workings, in the wrong hands this kind of information can lead to your site getting seriously p0wned. In order to prevent this, I don't even add the FirePHP writer to Zend_Log if I am not in a development environment. Here's how I accomplish this in my Workspace logger init method:

Here's what my controller plugin looks like:

The items that are logged are as follows:

1. The full paths to all view scripts that have been rendered - including layouts
2. The Module::controller::actions performed as well as how long it took to perform each action
3. An expandable table with the contents of the current user session
4. An expandable table of the current memcached stats. If memached has been disabled, it will indicate such. Numbers in parenthesis indicates the number of each operation that occured during the current request. This is a great way to ensure that the cache is actually working properly.
5. A timing of the requests entire processing time. This is basically measured as the difference between the dispatchLoopStartup and dispatchLoopShutdown events.
6. Any exceptions that were encountered are sent to the debug console with an expandable tree for the traceback.
7. The last and perhaps most import part of the debug console is the SQL query profiling. For each query that is run, the debug console dumps the query itself as well as the execution time for that SQL query. it also includes an explain plan of that query in an expandable table format so that you can see what decisions the MySQL optimizer made when it processed your query. This can be a quick way to look for "using key: none" or "temporary table" inefficiencies in your queries.

The added visibility into your application's inner workings in such a non-intrusive way are indispensable to me when developing. Once you use it in your own applications I am sure you will feel the same way.
Posted by Matthew Purdon in PHP Comments: (3) Trackbacks: (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

#1 - James 2008-12-23 02:16 - (Reply)

I can't get the RSS feed on your site to work...

#2 - Matthew Purdon 2009-01-05 09:03 - (Reply)

Everything should be good now - thanks for letting me know

#3 - James 2009-01-05 09:11 - (Reply)

Works, thank you :-)


Add Comment

Standard emoticons like :-) and ;-) are converted to images.
 
 

Subscribe

Archives

  • February 2010 (0)
  • January 2010 (4)
  • December 2009 (6)
  • November 2009 (0)
  • Recent...
  • Older...

Categories

  • XML Technology
  • XML Databases
  • XML MySQL
  • XML Software Development
  • XML Being a Contractor
  • XML Client Side
  • XML PHP
  • XML State of the Art


All categories

Blog Administration

Open login screen

Recommended Reading

Amazon.com: Patterns of Enterprise Application Architecture (0076092019909): Martin Fowler: Books
Amazon.com: Refactoring: Improving the Design of Existing Code (9780201485677): Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts: Books
Amazon.com: The Nomadic Developer: Surviving and Thriving in the World of Technology Consulting (9780321606396): Aaron Erickson: Books
Amazon.com: Domain-Driven Design: Tackling Complexity in the Heart of Software (0076092019565): Eric Evans: Books

Feedburner

Numeric Feedburner ID Required!




 

Layout by Andreas Viklund | Serendipity template by Carl