| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?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>
|