Zend Framework 101: Zend_Log
Debugging With Firebug and FirePHP
Zend_Log contains a log writer that will write messages to the HTTP headers of your web page response, which allows the Firebug plug-in for Firefox to show your log messages. This is an extremely powerful development tool, both for debugging and profiling your applications.
In order for this functionality to work you must be using Firefox with the following plug-ins installed:
- Firebug (plug-in for Firefox), available from http://www.getfirebug.com
- FirePHP (plug-in for Firebug), available from http://www.firephp.org
You must then use the bundled Zend_Log_Writer_Firebug writer. Listing 5 shows how this is achieved.
require_once('Zend/Log.php'); require_once('Zend/Log/Writer/Firebug.php'); $writer = new Zend_Log_Writer_Firebug(); $logger = new Zend_Log($writer); $logger->info('info message'); $logger->warn('warning message'); $logger->err('error message');
Note however that unless you're using Zend_Controller_Front with your site then nothing will happen when you use this code, as described in the next section.
Sending Log Messages
If you're using Zend_Controller_Front to manage requests to your web site then any log messages will automatically be sent to your browser in the HTTP headers.
If not, you must manually get the messages to be sent to your browser. The Zend_Log_Writer_Firebug class makes use of the Zend_Wildfire component, so you must tell this class about your HTTP response so headers can be inserted.
We must use the Zend_Controller_Request_Http and Zend_Controller_Response_Http classes and tell Zend_Wildfire about the request and response. This is so once the request has finished Zend_Wildfire will write the HTTP headers containing the logging information.
Listing 6 shows how we achieve this. The $channel->flush() call near the results in the headers being inserted into the response. We then output the headers using sendHeaders().
require_once('Zend/Log.php'); require_once('Zend/Log/Writer/Firebug.php'); require_once('Zend/Controller/Response/Http.php'); require_once('Zend/Controller/Request/Http.php'); // create the logger and log writer $writer = new Zend_Log_Writer_Firebug(); $logger = new Zend_Log($writer); // get the wildfire channel $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance(); // create and set the HTTP response $response = new Zend_Controller_Response_Http(); $channel->setResponse($response); // create and set the HTTP request $channel->setRequest(new Zend_Controller_Request_Http()); // record log messages $logger->info('info message'); $logger->warn('warning message'); $logger->err('error message'); // insert the wildfire headers into the HTTP response $channel->flush(); // send the HTTP response headers $response->sendHeaders();
If you then view this script in your browser (with Firebug and FirePHP enabled) you see the following.
Notice that the native Firebug message type indicators are used, and that the error also resulted in “1 error” being displayed at the bottom-right.




