PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using the Factory to Create a Log

In addition to direct instantiation, you may also use the static factory() method to instantiate a Log instance, as well as to configure attached writers and their filters. Using the factory, you can attach zero or more writers. Configuration may be passed as either an array or a Zend_Config instance.

As an example:

<?php
$logger 
Zend_Log::factory(array(
    
'timestampFormat' => 'Y-m-d',
    array(
        
'writerName'   => 'Stream',
        
'writerParams' => array(
            
'stream'   => '/tmp/zend.log',
        ),
        
'formatterName' => 'Simple',
        
'formatterParams' => array(
            
'format'   => '%timestamp%: %message% -- %info%',
        ),
        
'filterName'   => 'Priority',
        
'filterParams' => array(
            
'priority' => Zend_Log::WARN,
        ),
    ),
    array(
        
'writerName'   => 'Firebug',
        
'filterName'   => 'Priority',
        
'filterParams' => array(
            
'priority' => Zend_Log::INFO,
        ),
    ),
));

The above will instantiate a logger with two writers, one for writing to a local file, another for sending data to Firebug. Each has an attached priority filter, with different maximum priorities.

By default, events are logged with the ISO 8601 date format. You can choose your own format with the option timestampFormat.

Each writer can be defined with the following keys:

writerName (required)

The "short" name of a log writer; the name of the log writer minus the leading class prefix/namespace. See the "writerNamespace" entry below for more details. Examples: "Mock", "Stream", "Firebug".

writerParams (optional)

An associative array of parameters to use when instantiating the log writer. Each log writer's factory() method will map these to constructor arguments, as noted below.

writerNamespace (optional)

The class prefix/namespace to use when constructing the final log writer classname. By default, if this is not provided, "Zend_Log_Writer" is assumed; however, you can pass your own namespace if you are using a custom log writer.

formatterName (optional)

The "short" name of a formatter to use with the given log writer; the name of the formatter minus the leading class prefix/namespace. See the "formatterNamespace" entry below for more details. Examples: "Simple", "Xml".

formatterParams (optional)

An associative array of parameters to use when instantiating the log formatter. Each log formatter's factory() method will map these to constructor arguments, as noted below.

formatterNamespace (optional)

The class prefix/namespace to use when constructing the final log formatter classname. By default, if this is not provided, "Zend_Log_Formatter" is assumed; however, you can pass your own namespace if you are using a custom log formatter.

filterName (optional)

The "short" name of a filter to use with the given log writer; the name of the filter minus the leading class prefix/namespace. See the "filterNamespace" entry below for more details. Examples: "Message", "Priority".

filterParams (optional)

An associative array of parameters to use when instantiating the log filter. Each log filter's factory() method will map these to constructor arguments, as noted below.

filterNamespace (optional)

The class prefix/namespace to use when constructing the final log filter classname. By default, if this is not provided, "Zend_Log_Filter" is assumed; however, you can pass your own namespace if you are using a custom log filter.

Each writer and each filter has specific options.

Zend Framework