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

  1. Use full info ,it helped me alot

  2. If you return the Zend_Log instance in the function _initLog of the bootstrap, you will be able to get the logger with :

    $bootstrap->getResource(‘Log’)

    It’s the default behavior of the ErrorController.

    • Great stuff! Maybe someone wants to use this solution instead. Many thanks for taking the time to tell me

  3. hi Niklas,
    I noticed that the filterParams.priority parameter filters out higher numbered priorities (i.e. less severe problems)

    So to show all priorites you actually need to set resources.log.stream.filterParams.priority = 7

    Thanks for this easy guide!

    • You are absolutely right. I’ll change my post to make it easier to understand. Many thanks for your input

  4. Good article, thank you.
    Does this approach also work if you need several logs, let’s say an application log and an error log ?

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.