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
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.log.formatters">
<title>Formatters</title>
<para>
A Formatter is an object that is responsible for taking an <property>event</property> array
describing a log event and outputting a string with a formatted log line.
</para>
<para>
Some Writers are not line-oriented and cannot use a Formatter. An example is the
Database Writer, which inserts the event items directly into database columns. For
Writers that cannot support a Formatter, an exception is thrown if you attempt to
set a Formatter.
</para>
<sect2 id="zend.log.formatters.simple">
<title>Simple Formatting</title>
<para>
<classname>Zend_Log_Formatter_Simple</classname> is the default formatter. It is
configured automatically when you specify no formatter. The default configuration is
equivalent to the following:
</para>
<programlisting language="php"><![CDATA[
$format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple($format);
]]></programlisting>
<para>
A formatter is set on an individual Writer object using the Writer's
<methodname>setFormatter()</methodname> method:
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_Stream('php://output');
$formatter = new Zend_Log_Formatter_Simple('hello %message%' . PHP_EOL);
$writer->setFormatter($formatter);
$logger = new Zend_Log();
$logger->addWriter($writer);
$logger->info('there');
// outputs "hello there"
]]></programlisting>
<para>
The constructor of <classname>Zend_Log_Formatter_Simple</classname> accepts a single
parameter: the format string. This string contains keys surrounded by
percent signs (e.g. <command>%message%</command>). The format string may
contain any key from the event data array.
You can retrieve the default keys by using the DEFAULT_FORMAT constant from
<classname>Zend_Log_Formatter_Simple</classname>.
</para>
</sect2>
<sect2 id="zend.log.formatters.xml">
<title>Formatting to XML</title>
<para>
<classname>Zend_Log_Formatter_Xml</classname> formats log data into
<acronym>XML</acronym> strings. By default, it automatically logs all items in the event
data array:
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_Stream('php://output');
$formatter = new Zend_Log_Formatter_Xml();
$writer->setFormatter($formatter);
$logger = new Zend_Log();
$logger->addWriter($writer);
$logger->info('informational message');
]]></programlisting>
<para>
The code above outputs the following <acronym>XML</acronym> (space added for clarity):
</para>
<programlisting language="xml"><![CDATA[
<logEntry>
<timestamp>2007-04-06T07:24:37-07:00</timestamp>
<message>informational message</message>
<priority>6</priority>
<priorityName>INFO</priorityName>
</logEntry>
]]></programlisting>
<para>
It's possible to customize the root element as well as specify a mapping
of <acronym>XML</acronym> elements to the items in the event data array. The constructor
of <classname>Zend_Log_Formatter_Xml</classname> accepts a string with the name of
the root element as the first parameter and an associative array with
the element mapping as the second parameter:
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_Stream('php://output');
$formatter = new Zend_Log_Formatter_Xml('log',
array('msg' => 'message',
'level' => 'priorityName')
);
$writer->setFormatter($formatter);
$logger = new Zend_Log();
$logger->addWriter($writer);
$logger->info('informational message');
]]></programlisting>
<para>
The code above changes the root element from its default of
<property>logEntry</property> to <property>log</property>. It also maps the element
<property>msg</property> to the event data item <property>message</property>. This
results in the following output:
</para>
<programlisting language="xml"><![CDATA[
<log>
<msg>informational message</msg>
<level>INFO</level>
</log>
]]></programlisting>
</sect2>
</sect1>
|