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
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.log.writers" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Writers</title>
<para>
A Writer is an object that inherits from <classname>Zend_Log_Writer_Abstract</classname>.
A Writer's responsibility is to record log data to a storage backend.
</para>
<sect2 id="zend.log.writers.stream">
<title>Writing to Streams</title>
<para>
<classname>Zend_Log_Writer_Stream</classname> sends log
data to a <ulink url="http://www.php.net/stream">PHP stream</ulink>.
</para>
<para>
To write log data to the <acronym>PHP</acronym> output buffer, use the URL
<filename>php://output</filename>. Alternatively, you can send log data directly to a
stream like <constant>STDERR</constant> (<filename>php://stderr</filename>).
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_Stream('php://output');
$logger = new Zend_Log($writer);
$logger->info('Informational message');
]]></programlisting>
<para>
To write data to a file, use one of the
<ulink url="http://www.php.net/manual/en/wrappers.php#wrappers.file">Filesystem
URLs</ulink>:
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_Stream('/path/to/logfile');
$logger = new Zend_Log($writer);
$logger->info('Informational message');
]]></programlisting>
<para>
By default, the stream opens in the append mode ("a").
To open it with a different mode, the <classname>Zend_Log_Writer_Stream</classname>
constructor accepts an optional second parameter for the mode.
</para>
<para>
The constructor of <classname>Zend_Log_Writer_Stream</classname> also accepts an
existing stream resource:
</para>
<programlisting language="php"><![CDATA[
$stream = @fopen('/path/to/logfile', 'a', false);
if (! $stream) {
throw new Exception('Failed to open stream');
}
$writer = new Zend_Log_Writer_Stream($stream);
$logger = new Zend_Log($writer);
$logger->info('Informational message');
]]></programlisting>
<para>
You cannot specify the mode for existing stream resources. Doing so
causes a <classname>Zend_Log_Exception</classname> to be thrown.
</para>
</sect2>
<sect2 id="zend.log.writers.database">
<title>Writing to Databases</title>
<para>
<classname>Zend_Log_Writer_Db</classname> writes log information to a database table
using <classname>Zend_Db</classname>. The constructor of
<classname>Zend_Log_Writer_Db</classname> receives a
<classname>Zend_Db_Adapter</classname> instance, a table name, and a mapping of database
columns to event data items:
</para>
<programlisting language="php"><![CDATA[
$params = array ('host' => '127.0.0.1',
'username' => 'malory',
'password' => '******',
'dbname' => 'camelot');
$db = Zend_Db::factory('PDO_MYSQL', $params);
$columnMapping = array('lvl' => 'priority', 'msg' => 'message');
$writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping);
$logger = new Zend_Log($writer);
$logger->info('Informational message');
]]></programlisting>
<para>
The example above writes a single row of log data to the database table named
'log_table_name' table. The database column named 'lvl'
receives the priority number and the column named 'msg' receives the log message.
</para>
</sect2>
<xi:include href="Zend_Log-Writers-Firebug.xml" />
<xi:include href="Zend_Log-Writers-Mail.xml" />
<xi:include href="Zend_Log-Writers-Syslog.xml" />
<xi:include href="Zend_Log-Writers-ZendMonitor.xml" />
<sect2 id="zend.log.writers.null">
<title>Stubbing Out the Writer</title>
<para>
The <classname>Zend_Log_Writer_Null</classname> is a stub that does not write log data
to anything. It is useful for disabling logging or stubbing out logging during tests:
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_Null;
$logger = new Zend_Log($writer);
// goes nowhere
$logger->info('Informational message');
]]></programlisting>
</sect2>
<sect2 id="zend.log.writers.mock">
<title>Testing with the Mock</title>
<para>
The <classname>Zend_Log_Writer_Mock</classname> is a very simple writer that records
the raw data it receives in an array exposed as a public property.
</para>
<programlisting language="php"><![CDATA[
$mock = new Zend_Log_Writer_Mock;
$logger = new Zend_Log($mock);
$logger->info('Informational message');
var_dump($mock->events[0]);
// Array
// (
// [timestamp] => 2007-04-06T07:16:37-07:00
// [message] => Informational message
// [priority] => 6
// [priorityName] => INFO
// )
]]></programlisting>
<para>
To clear the events logged by the mock, simply set
<command>$mock->events = array()</command>.
</para>
</sect2>
<sect2 id="zend.log.writers.compositing">
<title>Compositing Writers</title>
<para>
There is no composite Writer object. However, a Log instance can write
to any number of Writers. To do this, use the <methodname>addWriter()</methodname>
method:
</para>
<programlisting language="php"><![CDATA[
$writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile');
$writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile');
$logger = new Zend_Log();
$logger->addWriter($writer1);
$logger->addWriter($writer2);
// goes to both writers
$logger->info('Informational message');
]]></programlisting>
</sect2>
</sect1>
|