1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.log.factory">
4 <title>Using the Factory to Create a Log</title>
7 In addition to direct instantiation, you may also use the static
8 <methodname>factory()</methodname> method to instantiate a Log instance, as well as to
9 configure attached writers and their filters. Using the factory, you can attach zero or
10 more writers. Configuration may be passed as either an array or a
11 <classname>Zend_Config</classname> instance.
18 <programlisting language="php"><![CDATA[
19 $logger = Zend_Log::factory(array(
21 'writerName' => 'Stream',
22 'writerParams' => array(
23 'stream' => '/tmp/zend.log',
25 'filterName' => 'Priority',
26 'filterParams' => array(
27 'priority' => Zend_Log::WARN,
31 'writerName' => 'Firebug',
32 'filterName' => 'Priority',
33 'filterParams' => array(
34 'priority' => Zend_Log::INFO,
41 The above will instantiate a logger with two writers, one for writing to a local file,
42 another for sending data to Firebug. Each has an attached priority filter, with different
47 Each writer can be defined with the following keys:
52 <term>writerName (required)</term>
56 The "short" name of a log writer; the name of the log writer minus the leading
57 class prefix/namespace. See the "writerNamespace" entry below for more details.
58 Examples: "Mock", "Stream", "Firebug".
64 <term>writerParams (optional)</term>
68 An associative array of parameters to use when instantiating the log writer.
69 Each log writer's <methodname>factory()</methodname> method will map these to
70 constructor arguments, as noted below.
76 <term>writerNamespace (optional)</term>
80 The class prefix/namespace to use when constructing the final log writer
81 classname. By default, if this is not provided, "Zend_Log_Writer" is assumed;
82 however, you can pass your own namespace if you are using a custom log writer.
88 <term>filterName (optional)</term>
92 The "short" name of a filter to use with the given log writer; the name of the
93 filter minus the leading class prefix/namespace. See the "filterNamespace" entry
94 below for more details. Examples: "Message", "Priority".
100 <term>filterParams (optional)</term>
104 An associative array of parameters to use when instantiating the log filter.
105 Each log filter's <methodname>factory()</methodname> method will map these to
106 constructor arguments, as noted below.
112 <term>filterNamespace (optional)</term>
116 The class prefix/namespace to use when constructing the final log filter
117 classname. By default, if this is not provided, "Zend_Log_Filter" is assumed;
118 however, you can pass your own namespace if you are using a custom log filter.
125 Each writer and each filter has specific options.
128 <sect2 id="zend.log.factory.writer-options">
129 <title>Writer Options</title>
131 <sect3 id="zend.log.factory.writer-options.db">
132 <title>Zend_Log_Writer_Db Options</title>
140 A <classname>Zend_Db_Adapter</classname> instance.
150 The name of the table in the RDBMS that will contain log entries.
156 <term>columnMap</term>
160 An associative array mapping database table column names to log event
168 <sect3 id="zend.log.factory.writer-options.firebug">
169 <title>Zend_Log_Writer_Firebug Options</title>
172 This log writer takes no options; any provided will be ignored.
176 <sect3 id="zend.log.factory.writer-options.mail">
177 <title>Zend_Log_Writer_Mail Options</title>
180 <classname>Zend_Log_Writer_Mail</classname> currently (as of 1.10) does not
181 implement a factory, and will raise an exception if you attempt to instantiate it
182 via <methodname>Zend_Log::factory()</methodname>.
186 <sect3 id="zend.log.factory.writer-options.mock">
187 <title>Zend_Log_Writer_Mock Options</title>
190 This log writer takes no options; any provided will be ignored.
194 <sect3 id="zend.log.factory.writer-options.null">
195 <title>Zend_Log_Writer_Null Options</title>
198 This log writer takes no options; any provided will be ignored.
202 <sect3 id="zend.log.factory.writer-options.stream">
203 <title>Zend_Log_Writer_Stream Options</title>
207 <term>stream|url</term>
211 A valid <acronym>PHP</acronym> stream identifier to which to log.
221 The I/O mode with which to log; defaults to "a", for "append".
228 <sect3 id="zend.log.factory.writer-options.syslog">
229 <title>Zend_Log_Writer_Syslog Options</title>
233 <term>application</term>
237 Application name used by the syslog writer.
243 <term>facility</term>
247 Facility used by the syslog writer.
254 <sect3 id="zend.log.factory.writer-options.zendmonitor">
255 <title>Zend_Log_Writer_ZendMonitor Options</title>
258 This log writer takes no options; any provided will be ignored.
263 <sect2 id="zend.log.factory.filter-options">
264 <title>Filter Options</title>
266 <sect3 id="zend.log.factory.filter-options.message">
267 <title>Zend_Log_Filter_Message Options</title>
275 Regular expression that must be matched in order to log a message.
282 <sect3 id="zend.log.factory.filter-options.priority">
283 <title>Zend_Log_Filter_Priority Options</title>
287 <term>priority</term>
291 The maximum priority level by which messages will be logged.
297 <term>operator</term>
301 The comparison operator by which to do priority comparisons; defaults to
309 <sect3 id="zend.log.factory.filter-options.suppress">
310 <title>Zend_Log_Writer_Suppress Options</title>
313 This log filter takes no options; any provided will be ignored.
318 <sect2 id="zend.log.factory.custom">
319 <title>Creating Configurable Writers and Filters</title>
322 If you find yourself needing to write your own log writers and/or filters, you can make
323 them compatible with <methodname>Zend_Log::factory()</methodname> very easily.
327 At the minimum, you need to implement
328 <interfacename>Zend_Log_FactoryInterface</interfacename>, which expects a static
329 <methodname>factory()</methodname> method that accepts a single argument,
330 <varname>$config</varname>, which may be either an array or
331 <classname>Zend_Config</classname> object. If your log writer extends
332 <classname>Zend_Log_Writer_Abstract</classname>, or your log filter extends
333 <classname>Zend_Log_Filter_Abstract</classname>, you will pick this up for free.
337 Then, simply define mappings between the accepted configuration and any constructor
338 arguments. As an example:
341 <programlisting language="php"><![CDATA[
342 class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
344 public function __construct($bar, $baz)
349 public static function factory($config)
351 if ($config instanceof Zend_Config) {
352 $config = $config->toArray();
354 if (!is_array($config)) {
356 'factory expects an array or Zend_Config instance'
364 $config = array_merge($default, $config);
375 Alternately, you could call appropriate setters after instantiation, but prior to
376 returning the instance:
379 <programlisting language="php"><![CDATA[
380 class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
382 public function __construct($bar = null, $baz = null)
387 public function setBar($value)
392 public function setBaz($value)
397 public static function factory($config)
399 if ($config instanceof Zend_Config) {
400 $config = $config->toArray();
402 if (!is_array($config)) {
404 'factory expects an array or Zend_Config instance'
408 $writer = new self();
409 if (isset($config['bar'])) {
410 $writer->setBar($config['bar']);
412 if (isset($config['baz'])) {
413 $writer->setBaz($config['baz']);