by Kevin Schroeder | 12:00 am

So I was sitting here thinking to myself “This is Friday and I’m not getting much of anything done.  Maybe I should write another Friday Framework Highlight.”  I figured that it was a good idea so I pondered what I should write.  I came up blank and so I asked Matthew Weier O’Phinney.  “Multiple writers for Zend_Log,” he said.  I agreed.

If you were not aware, Zend_Log provides facilities for writing to multiple logs through the same log instance.  Additionally, you can do this via configuration options when using a Zend_Applicatin resource plugin.  Together those make for very powerful logging mechanisms.  “How?” you ask?  It’s really easy.  Take your application.ini file, which you use to configure your Zend_Application instance, and make it look something like this.  I’ll highlight the pertinent parts

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0 = "Stream" = APPLICATION_PATH "/logs/application.log" = "a" = "Priority" = 4

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1 = 7
resources.frontController.params.displayExceptions = 1
resources.log.firebug.writerName = "FireBug"
resources.log.firebug.filterName = "Priority"
resources.log.firebug.filterParams.priority = 7


What this does is say that “in production, log warnings and above to the log file, but in development, log debug to the log file AND send the log items to FirePHP.”  Then, in our index controller we put this:

class IndexController extends Zend_Controller_Action

    public function indexAction()
        $this->getInvokeArg('bootstrap')->log->debug("I'm at indexAction");



When we execute this code we get both the output in the application log

$ tail -f application.log
2010-09-10T16:27:25-05:00 DEBUG (7): I'm at indexAction

and in the Firebug log

X-Wf-1-1-1-1            122|[{"Type":"LOG","File":"C:\workspace\Test\application\controllers\IndexController.php","Line":8},"I'm at indexAction"]|

In production we wouldn’t get anything since this code would filter out debug logging, due to the setting in the production section in application.ini.  Simple.  Done.

Tags: ,


Anton Visser

Kevin, this is great info, thanks for sharing this.

One thing, I had to change is resources.log.firebug.writerName = “FireBug” to resources.log.firebug.writerName = “Firebug” to make the autoloader happy as the file name is Firebug.php

Sep 15.2010 | 01:22 pm


Thanks for the catch. I wrote the example on Windows which probably didn’t care.

Sep 15.2010 | 01:23 pm

Anton Visser

The getInvokeArg() method works great in Controllers, but how do you access the log stream in a model?

Sep 15.2010 | 02:08 pm

Anton Visser

I found a solution which makes use of the registry:

Sep 15.2010 | 02:25 pm

Leave a Reply

Your email address will not be published. Required fields are marked *