Create a logger in Zend Framework using application.ini

I am here going to show how I usually setup a logger in my Zend applications. First we add the logger settings into the application.ini:

[production]
...
;Setup logger (file)
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/logs/basic.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 7
...
  • WriterName is the type of writer you want. I choose “Stream” here for logging to file
  • WriterParams is the parameters which are sent to the writer. Since i use stream i set the path to the logfile and set the “mode” to “a” (append to file)
  • FilterName is the name of the filter type. I use “Priority” so I can use loglevels
  • FilterParams is set to 7 (Debug) so that all loglevels are logged

Now we will make the logger easily accessible by putting a handle in the Zend register like this.
Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
...
    protected function _initLogger() {
        $this->bootstrap("log");
        $logger = $this->getResource("log");
        Zend_Registry::set("logger", $logger);
    }
...
}

Now, to use the logger in your application just do like this:


//Get the logger handle from the register
$logger = Zend_Registry::get('logger');

//Use the logger
$logger->log("message", 3);//Second argument is the loglevel (0-7)

Valid loglevels (straight from the Zend code:Log.php):

EMERG   = 0;  // Emergency: system is unusable
ALERT   = 1;  // Alert: action must be taken immediately
CRIT    = 2;  // Critical: critical conditions
ERR     = 3;  // Error: error conditions
WARN    = 4;  // Warning: warning conditions
NOTICE  = 5;  // Notice: normal but significant condition
INFO    = 6;  // Informational: informational messages
DEBUG   = 7;  // Debug: debug messages

Tested on Mac OSX 10.6.8 and Zend Framework 1.10.8

Comments are closed.