FirePHP and Zend Framework 1.6



 

The long awaited day has arrived. Zend Framework 1.6 just shipped and with it native support for FirePHP! This means that if you use Zend Framework (ZF) you can now log to your Firebug Console via FirePHP without needing to download any other PHP files such as the FirePHPCore library. There are two primary components available at this time. One is the Zend_Log_Writer_Firebug and the other the Zend_Db_Profiler_Firebug.

Lets check out how to use these new components. I would think that most ZF applications use Zend_Controller_Front so the following examples assume you have a basic MVC application.

Install Firebug and FirePHP

You first need to install the Firebug and FirePHP Firefox extensions. You can install them from here:

Logging to FirePHP with Zend_Log

All logging in ZF is done via Zend_Log which delegates the actual logging to registered log writers. The Zend_Log_Writer_Firebug component is such a writer and as the name indicates it sends your log data to Firebug.

Place the following code in your bootstrap file somewhere before dispatching your front controller:

$logger = new Zend_Log();
$writer = new Zend_Log_Writer_Firebug();
$logger->addWriter($writer);

To keep an instance of your logger you can use:

Zend_Registry::set('logger',$logger);

The Firebug log writer is all setup and ready to go. You can now dispatch your front controller and log data from within any of your model, view and controller files with the following:

$logger = Zend_Registry::get('logger');
$logger->log('This is a log message!', Zend_Log::INFO);

If you have done everything correctly you should see “This is a log message!” in your Firebug Console. If it is not showing up, make sure you have the Net and Console Firebug panels enabled.

The Zend_Log::INFO constant passed to log() determines the priority of the logging message. To increase the priority you can use Zend_Log::WARN or Zend_Log::ALERT for example which will produce a log message with a warning and error style respectively.

To log a plain message without any icon in Firebug you can use Zend_Log::DEBUG.

While logging simple strings is fun, FirePHP really comes into its own when logging complex data structures such as multi-dimensional arrays and objects. All you need to do is pass the object you want to log to the log() method, specify the logging priority and FirePHP will render the data appropriately.

For example you can log your exceptions with a complete expandable stack trace with the following code:

try {
// Code that may throw an exception
} catch(Exception $e) {
Zend_Registry::get('logger')->err($e)
}

Note the above code also uses the err() shortcut instead of passing the Zend_Log::Err constant to log().

For extensive documentation on how to use the Firebug log writer including information on how to create tables in the Firebug Console please see the online documentation.

Profiling Database Queries

There are numerous reasons for wanting to see a list of database queries executed during a page request. The Zend_Db_Profiler_Firebug component makes this possible and displays the result in an expandable table in the Firebug Console.

Place the following code in your bootstrap file somewhere after your database adapter ($db) is initialized and before dispatching your front controller:

$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
$profiler->setEnabled(true);
$db->setProfiler($profiler);

That’s it! Any database queries executed in your model, view and controller files will now be sent to Firebug along with the time it took to execute each, the query that was executed and any parameters bound to the query.

For a more detailed tutorial on how to use Zend_Db_Proflier_Firebug see here.

Try it Out!

You may be used to debugging your ZF code using Zend_Debug::dump() or even var_dump(). What do you have to loose trying these new components? They offer the big advantage of not dumping your debug data to the page. They allow you to debug applications with intricate designs as well as AJAX request that require clean JSON or XML responses.

Even if you are used to xDebug, FirePHP may prove to be a faster debugging solution in some cases than starting a debug session every time you need to ge some quick feedback as to what your variables are doing.

If you get tired of typing Zend_Registry::get(’logger’)->debug(); every time you need to print some debug data you can place the following wrapper function in your bootstrap file:

function fb($message, $label=null)
{
if ($label!=null) {
$message = array($label,$message);
}
Zend_Registry::get('logger')->debug($message);
}

Now you can simply use:

fb('This is a log message!');
fb($_SERVER, 'Server Variables');

One last note of caution. Do not log your debug data for everyone to see! Make sure you disable all logging when your application runs in production. Instead of removing your logging code every time you upload your application, you can disable the log writer and database profiler in your bootstrap file with:

$writer->setEnabled(false);
$profiler->setEnabled(false);

More Information/Help

For detailed information on these two components please refer to the Zend Framework Manual. Specifically the Zend_Log_Writer_Firebug and Zend_Db_Profiler_Firebug chapters.

If you need help use one of the following forums:


 

Netdip.com is an excellent web site that's powered by TYPO3 and other great open source software. Netdip.com is also a fat free alternative to ice cream.