Firebug has a fantastic tool for logging messages to the console. In PHP, we often use the error_log function to achieve a similar goal. However, this means that we have to tail the php error log to see these errors. Due to sloppy programming in the past many PHP error logs can become very clogged up and, unless you use some fancy grep technique, unusable.
Enter FirePHP & Zend_Log_Writer_Firebug…
A project has been around for some time now called FirePHP. This enables us to log messages of various levels of severity to the Firebug console! – Awsome. Just imagine if you are developing an ajax app, you could have all your debug messaging in one place.
This can either be used stand-alone using the classes provided, or Zend Framework have some amazing built-in tools for exactly this purpose.
Since I develop everything these days in Zend Framework, I’m going to concentrate this article on using some basic features of using FirePHP and the Firebug console with your Zend Framework application.
For the purposes of this article, I am asuming that you are using a standard Zend Framework project layout like this one and you are using Firefox.
Step 1) Install Firebug & FirePHP…
The most important step is to install Firebug and then install FirePHP. Once these two are installed, you will need to restart Firefox. So bookmark this page before installing them – a good time to do that would be now.
- Install Firebug – Click Here
- Install FirePHP – Click Here
Step 2) Edit Bootstrap.php file…
Edit your bootstrap file and add the _initLogging method to enable logging as follows:
protected function _initLogging() { $this->bootstrap('frontController'); $logger = new Zend_Log(); if ('development' == $this->getEnvironment()) { /** * Setting the logger to firebug when in development - Awsome! */ $logger->addWriter(new Zend_Log_Writer_Firebug()); } else { /** * Here, we setup the writer to write to null. This * ensures that we are not doing anything on live. * * @link http://framework.zend.com/manual/en/zend.log.writers.html#zend.log.writers.null */ $logger->addWriter(new Zend_Log_Writer_Null()); } Zend_Registry::set('logger', $logger); }
Step 3) Use the logger in your application…
Open any part of your application, the best place would be an action, and paste the following:
Zend_Registry::get('logger')->info('Info message'); Zend_Registry::get('logger')->warn('Warn message'); Zend_Registry::get('logger')->error('Error message');
Step 4) Open Firebug
Click on the Firebug icon. The icon, if you’ve never seen it before, looks like a little bug and is on the Firefox status bar. Once opened, Firebug will appear along the bottom of the browser. Click the “Console” button – it may ask you to “Enable for this website”.
Step 5) In action…
Open your website and naviage to the action where you pasted the code from step 3. You should see the following:

You’re done! You can now log whatever you want to Firebug’s console. Here is an example of the different levels of severity you can report on:
Zend_Registry::get('logger')->emerg('Emergency firebug message'); Zend_Registry::get('logger')->alert('Alert firebug message'); Zend_Registry::get('logger')->crit('Critical firebug message'); Zend_Registry::get('logger')->err('Error firebug message'); Zend_Registry::get('logger')->warn('Warning firebug message'); Zend_Registry::get('logger')->notice('Notice firebug message'); Zend_Registry::get('logger')->info('Info firebug message'); Zend_Registry::get('logger')->debug('Debug firebug message');
One step further?
When I implemented this, I took it one step further. I made the following change to the _initLogging method in the bootstrap as follows:
protected function _initLogging() { $this->bootstrap('frontController'); $logger = new Zend_Log(); $env = $this->getEnvironment(); $writer = 'production' == $env ? new Zend_Log_Writer_Stream('/tmp/critical_php.log') : new Zend_Log_Writer_Firebug(); $logger->addWriter($writer); if ('production' == $env) { $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT); $logger->addFilter($filter); } Zend_Registry::set('logger', $logger); }
The code above ensures that in non-production mode all these errors are written to Firebug and in production model only errors with severity of critical and above are written to my custom error log. You would probably want to find a better place to put your log file and do some housekeeping to remove old entries.
Zend_Log_Writer also have a few other adapters for your to use including Zend_Log_Writer_Db for writing log messages to a database table, Zend_Log_Writer_Mail for sending log messages by emails, Zend_Log_Writer_Mock for unit testing, Zend_Log_Writer_Syslog for writing log messages to the syslog.
If you use bug tracking software such as Jira or Trac, and you have an XML-RPC pluign installed, you could probably create your own Zend_Log_Writer by extending the Zend_Log_Writer_Abstract class.
I also found this article about FirePHP and Zend Framework a very interesting read.
Pingback: Henry Hayes ‘Blog: Firebug console.log für PHP mit Zend Framework | PHP Boutique
Pingback: Henry Hayes’ Blog: Firebug Console.Log for PHP using Zend Framework » Steven's Blog