1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.log.factory">
<title>Using the Factory to Create a Log</title>
<para>
In addition to direct instantiation, you may also use the static
<methodname>factory()</methodname> 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
<classname>Zend_Config</classname> instance. If you want to create an instance of
a custom class (extending Zend_Log), you can pass a <varname>className</varname>
option to the <methodname>factory()</methodname> method.
</para>
<para>
As an example:
</para>
<programlisting language="php"><![CDATA[
$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,
),
),
));
]]></programlisting>
<para>
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.
</para>
<para>
By default, events are logged with the ISO 8601 date format. You can choose your own format
with the option <emphasis>timestampFormat</emphasis>.
</para>
<para>
Each writer can be defined with the following keys:
</para>
<variablelist>
<varlistentry>
<term>writerName (required)</term>
<listitem>
<para>
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".
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>writerParams (optional)</term>
<listitem>
<para>
An associative array of parameters to use when instantiating the log writer.
Each log writer's <methodname>factory()</methodname> method will map these to
constructor arguments, as noted below.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>writerNamespace (optional)</term>
<listitem>
<para>
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.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>formatterName (optional)</term>
<listitem>
<para>
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".
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>formatterParams (optional)</term>
<listitem>
<para>
An associative array of parameters to use when instantiating the log formatter.
Each log formatter's <methodname>factory()</methodname> method will map these to
constructor arguments, as noted below.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>formatterNamespace (optional)</term>
<listitem>
<para>
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.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>filterName (optional)</term>
<listitem>
<para>
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".
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>filterParams (optional)</term>
<listitem>
<para>
An associative array of parameters to use when instantiating the log filter.
Each log filter's <methodname>factory()</methodname> method will map these to
constructor arguments, as noted below.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>filterNamespace (optional)</term>
<listitem>
<para>
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.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
Each writer and each filter has specific options.
</para>
<sect2 id="zend.log.factory.writer-options">
<title>Writer Options</title>
<sect3 id="zend.log.factory.writer-options.db">
<title>Zend_Log_Writer_Db Options</title>
<variablelist>
<varlistentry>
<term>db</term>
<listitem>
<para>
A <classname>Zend_Db_Adapter</classname> instance.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>table</term>
<listitem>
<para>
The name of the table in the RDBMS that will contain log entries.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>columnMap</term>
<listitem>
<para>
An associative array mapping database table column names to log event
fields.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="zend.log.factory.writer-options.firebug">
<title>Zend_Log_Writer_Firebug Options</title>
<para>
This log writer takes no options; any provided will be ignored.
</para>
</sect3>
<sect3 id="zend.log.factory.writer-options.mail">
<title>Zend_Log_Writer_Mail Options</title>
<table id="zend.log.factory.writer-options.mail.table">
<title>Zend_Log_Writer_Mail Options</title>
<tgroup cols="4">
<thead>
<row>
<entry>Option</entry>
<entry>Data Type</entry>
<entry>Default Value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><emphasis>mail</emphasis></entry>
<entry><type>String</type></entry>
<entry><classname>Zend_Mail</classname></entry>
<entry>
An <classname>Zend_Mail</classname> instance
</entry>
</row>
<row>
<entry><emphasis>charset</emphasis></entry>
<entry><type>String</type></entry>
<entry>iso-8859-1</entry>
<entry>
Charset of the mail
</entry>
</row>
<row>
<entry><emphasis>from</emphasis></entry>
<entry><type>String</type> or <type>Array</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
Sender of the mail
The parameters for <type>Array</type> type are :
<itemizedlist>
<listitem>
<para>
<property>email</property> : address of sender
</para>
</listitem>
<listitem>
<para>
<property>name</property> : name of sender
</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry><emphasis>to</emphasis></entry>
<entry><type>String</type> or <type>Array</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
Recipient(s) of the mail
</entry>
</row>
<row>
<entry><emphasis>cc</emphasis></entry>
<entry><type>String</type> or <type>Array</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
Carbon copy recipient(s) of the mail
</entry>
</row>
<row>
<entry><emphasis>bcc</emphasis></entry>
<entry><type>String</type> or <type>Array</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
Blind carbon copy recipient(s) of the mail
</entry>
</row>
<row>
<entry><emphasis>subject</emphasis></entry>
<entry><type>String</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
Subject of the mail
</entry>
</row>
<row>
<entry><emphasis>subjectPrependText</emphasis></entry>
<entry><type>String</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
A summary of number of errors per priority is appended to the
subject of the mail
</entry>
</row>
<row>
<entry><emphasis>layout</emphasis></entry>
<entry><type>String</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
An <classname>Zend_Layout</classname> instance
</entry>
</row>
<row>
<entry><emphasis>layoutOptions</emphasis></entry>
<entry><type>Array</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
See the section <xref linkend="zend.layout.options" />
</entry>
</row>
<row>
<entry><emphasis>layoutFormatter</emphasis></entry>
<entry><type>String</type></entry>
<entry><constant>NULL</constant></entry>
<entry>
An <classname>Zend_Log_Formatter_Interface</classname> instance
</entry>
</row>
</tbody>
</tgroup>
</table>
</sect3>
<sect3 id="zend.log.factory.writer-options.mock">
<title>Zend_Log_Writer_Mock Options</title>
<para>
This log writer takes no options; any provided will be ignored.
</para>
</sect3>
<sect3 id="zend.log.factory.writer-options.null">
<title>Zend_Log_Writer_Null Options</title>
<para>
This log writer takes no options; any provided will be ignored.
</para>
</sect3>
<sect3 id="zend.log.factory.writer-options.stream">
<title>Zend_Log_Writer_Stream Options</title>
<variablelist>
<varlistentry>
<term>stream|url</term>
<listitem>
<para>
A valid <acronym>PHP</acronym> stream identifier to which to log.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>mode</term>
<listitem>
<para>
The I/O mode with which to log; defaults to "a", for "append".
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="zend.log.factory.writer-options.syslog">
<title>Zend_Log_Writer_Syslog Options</title>
<variablelist>
<varlistentry>
<term>application</term>
<listitem>
<para>
Application name used by the syslog writer.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>facility</term>
<listitem>
<para>
Facility used by the syslog writer.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="zend.log.factory.writer-options.zendmonitor">
<title>Zend_Log_Writer_ZendMonitor Options</title>
<para>
This log writer takes no options; any provided will be ignored.
</para>
</sect3>
</sect2>
<sect2 id="zend.log.factory.filter-options">
<title>Filter Options</title>
<sect3 id="zend.log.factory.filter-options.message">
<title>Zend_Log_Filter_Message Options</title>
<variablelist>
<varlistentry>
<term>regexp</term>
<listitem>
<para>
Regular expression that must be matched in order to log a message.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="zend.log.factory.filter-options.priority">
<title>Zend_Log_Filter_Priority Options</title>
<variablelist>
<varlistentry>
<term>priority</term>
<listitem>
<para>
The maximum priority level by which messages will be logged.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>operator</term>
<listitem>
<para>
The comparison operator by which to do priority comparisons; defaults to
"<=".
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="zend.log.factory.filter-options.suppress">
<title>Zend_Log_Filter_Suppress Options</title>
<para>
This log filter takes no options; any provided will be ignored.
</para>
</sect3>
</sect2>
<sect2 id="zend.log.factory.custom">
<title>Creating Configurable Writers and Filters</title>
<para>
If you find yourself needing to write your own log writers and/or filters, you can make
them compatible with <methodname>Zend_Log::factory()</methodname> very easily.
</para>
<para>
At the minimum, you need to implement
<interfacename>Zend_Log_FactoryInterface</interfacename>, which expects a static
<methodname>factory()</methodname> method that accepts a single argument,
<varname>$config</varname>, which may be either an array or
<classname>Zend_Config</classname> object. If your log writer extends
<classname>Zend_Log_Writer_Abstract</classname>, or your log filter extends
<classname>Zend_Log_Filter_Abstract</classname>, you will pick this up for free.
</para>
<para>
Then, simply define mappings between the accepted configuration and any constructor
arguments. As an example:
</para>
<programlisting language="php"><![CDATA[
class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
{
public function __construct($bar, $baz)
{
// ...
}
public static function factory($config)
{
if ($config instanceof Zend_Config) {
$config = $config->toArray();
}
if (!is_array($config)) {
throw new Exception(
'factory expects an array or Zend_Config instance'
);
}
$default = array(
'bar' => null,
'baz' => null,
);
$config = array_merge($default, $config);
return new self(
$config['bar'],
$config['baz']
);
}
}
]]></programlisting>
<para>
Alternately, you could call appropriate setters after instantiation, but prior to
returning the instance:
</para>
<programlisting language="php"><![CDATA[
class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
{
public function __construct($bar = null, $baz = null)
{
// ...
}
public function setBar($value)
{
// ...
}
public function setBaz($value)
{
// ...
}
public static function factory($config)
{
if ($config instanceof Zend_Config) {
$config = $config->toArray();
}
if (!is_array($config)) {
throw new Exception(
'factory expects an array or Zend_Config instance'
);
}
$writer = new self();
if (isset($config['bar'])) {
$writer->setBar($config['bar']);
}
if (isset($config['baz'])) {
$writer->setBaz($config['baz']);
}
return $writer;
}
}
]]></programlisting>
</sect2>
</sect1>
|