Filters | Zend_Log Programmer's Reference Guide | Zend_Mail

# 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:

1. $logger = Zend_Log::factory(array(
2. array(
3. 'writerName' => 'Stream',
4. 'writerParams' => array(
5. 'stream' => '/tmp/zend.log',
6. ),
7. 'filterName' => 'Priority',
8. 'filterParams' => array(
9. 'priority' => Zend_Log::WARN,
10. ),
11. ),
12. array(
13. 'writerName' => 'Firebug',
14. 'filterName' => 'Priority',
15. 'filterParams' => array(
16. 'priority' => Zend_Log::INFO,
17. ),
18. ),
19. ));

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.

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.

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.

# Writer Options

# Zend_Log_Writer_Db Options

db

  A Zend_Db_Adapter instance.

table

  The name of the table in the RDBMS that will contain log entries.

columnMap

  An associative array mapping database table column names to log event fields.

# Zend_Log_Writer_Firebug Options

This log writer takes no options; any provided will be ignored.

# Zend_Log_Writer_Mail Options

Zend_Log_Writer_Mail currently (as of 1.10) does not implement a factory, and will raise an exception if you attempt to instantiate it via Zend_Log::factory().

# Zend_Log_Writer_Mock Options

This log writer takes no options; any provided will be ignored.

# Zend_Log_Writer_Null Options

This log writer takes no options; any provided will be ignored.

# Zend_Log_Writer_Stream Options

stream|url

  A valid PHP stream identifier to which to log.

mode

  The I/O mode with which to log; defaults to "a", for "append".

# Zend_Log_Writer_Syslog Options

application

  Application name used by the syslog writer.

facility

  Facility used by the syslog writer.

# Zend_Log_Writer_ZendMonitor Options

This log writer takes no options; any provided will be ignored.

# Filter Options

# Zend_Log_Filter_Message Options

regexp

  Regular expression that must be matched in order to log a message.

# Zend_Log_Filter_Priority Options

priority

  The maximum priority level by which messages will be logged.

operator

  The comparison operator by which to do priority comparisons; defaults to "<=".

# Zend_Log_Writer_Suppress Options

This log filter takes no options; any provided will be ignored.

# Creating Configurable Writers and Filters

If you find yourself needing to write your own log writers and/or filters, you can make them compatible with Zend_Log::factory() very easily.

At the minimum, you need to implement Zend_Log_FactoryInterface, which expects a static factory() method that accepts a single argument, $config, which may be either an array or Zend_Config object. If your log writer extends Zend_Log_Writer_Abstract, or your log filter extends Zend_Log_Filter_Abstract, you will pick this up for free.

Then, simply define mappings between the accepted configuration and any constructor arguments. As an example:

1. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
2. {
3. public function __construct($bar, $baz)
4. {
5. // ...
6. }
8. public static function factory($config)
9. {
10. if ($config instanceof Zend_Config) {
11. $config = $config->toArray();
12. }
13. if (!is_array($config)) {
14. throw new Exception(
15. 'factory expects an array or Zend_Config instance'
16. );
17. }
19. $default = array(
20. 'bar' => null,
21. 'baz' => null,
22. );
23. $config = array_merge($default, $config);
25. return new self(
26. $config['bar'],
27. $config['baz']
28. );
29. }
30. }

Alternately, you could call appropriate setters after instantiation, but prior to returning the instance:

1. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
2. {
3. public function __construct($bar = null, $baz = null)
4. {
5. // ...
6. }
8. public function setBar($value)
9. {
10. // ...
11. }
13. public function setBaz($value)
14. {
15. // ...
16. }
18. public static function factory($config)
19. {
20. if ($config instanceof Zend_Config) {
21. $config = $config->toArray();
22. }
23. if (!is_array($config)) {
24. throw new Exception(
25. 'factory expects an array or Zend_Config instance'
26. );
27. }
29. $writer = new self();
30. if (isset($config['bar'])) {
31. $writer->setBar($config['bar']);
32. }
33. if (isset($config['baz'])) {
34. $writer->setBaz($config['baz']);
35. }
36. return $writer;
37. }
38. }

Filters | Zend_Log Programmer's Reference Guide | Zend_Mail