If you use Firefox, I bet you already know Firebug extension. This is irreplaceable tool for web development. But there is also FirePHP extension, which provides possibility to log into Firebug console from PHP scripts. This is very convenient way for debugging process, because logs are independent from application output.
Zend Framework has really neat logging component, Zend_Log. Zend_Log is decoupled into four objects: Log, Writer, Filter and Formatter. Each of object is exchangeable. Writer is responsible for saving data. So it means, that we can save our logs anywhere we want. Into syslog, database or .. FirePHP console – we only need to use appropriate Writer. Luckily if we want to write to FirePHP console, we don’t have to create such Writer alone! In Zend Framework we can found FirebugWriter.
Using Zend_Log with FirePHP is really straightforward.
# configure logger $logger = new Zend_Log(); $writer = new Zend_Log_Writer_Firebug(); $logger->addWriter($writer); # log into FirePHP console $logger->log('Hello 1!', Zend_Log::INFO); $logger->log('Hello 2!', Zend_Log::WARN); $logger->log('Hello 3!', Zend_Log::ERR);
JSON Action Helper – sendJson method
By default, JSON Action Helper sendJson method won’t work with Firebug Writer. sendJson method sends response and invoke exit function. To change this behavior (avoid exit function) , we need to set JSON Action Helper suppressExit property to true.
Writing tabular data into Firebug console
Sometimes we need to log tabular data. For example, when we use database profiler. We can achieve this with Zend_Wildfire component and Zend_Wildfire_Plugin_FirePhp_TableMessage plugin.
Example of logging tabular data.
$this->_message = new \Zend_Wildfire_Plugin_FirePhp_TableMessage('Test'); $this->_message->setBuffered(true); $this->_message->setHeader(array('One','Two','Three')); $this->_message->setOption('includeLineNumbers', false); $this->_message->addRow(array(1,2,3)); $this->_message->addRow(array(4,5,6)); Zend_Wildfire_Plugin_FirePhp::getInstance()->send($this->_message, 'Test');