1 <sect1 id="zend.log.writers" xmlns:xi="http://www.w3.org/2001/XInclude">
4 一个Writer是继承自<code>Zend_Log_Writer_Abstract</code>类的一个对象.Writer的职责是向存储后端纪录日志数据.
7 <sect2 id="zend.log.writers.stream">
8 <title>写入到流(Streams)</title>
10 <code>Zend_Log_Writer_Stream</code> 写入日志数据到
11 <ulink url="http://www.php.net/stream">PHP 流</ulink>中.
15 要把日志写入到输出缓冲区,使用URL<code>php:://output</code>. 或则你可以直接发送日志数据到像<code>STDERR</code>
18 <programlisting role="php"><![CDATA[
19 $writer = new Zend_Log_Writer_Stream('php://output');
20 $logger = new Zend_Log($writer);
22 $logger->info('Informational message');
28 写入日志数据到一个文件,使用<ulink url="http://www.php.net/manual/en/wrappers.php#wrappers.file">Filesystem
31 <programlisting role="php"><![CDATA[
32 $writer = new Zend_Log_Writer_Stream('/path/to/logfile');
33 $logger = new Zend_Log($writer);
35 $logger->info('Informational message');
39 默认情况流一个追加(<code>"a"</code>)的方式打开.要以不同的模式打开,Zend_Log_Writer_Stream
44 <code>Zend_Log_Writer_Stream</code>还接受一个现有的流资源:
46 <programlisting role="php"><![CDATA[
47 $stream = @fopen('/path/to/logfile', 'a', false);
49 throw new Exception('Failed to open stream');
52 $writer = new Zend_Log_Writer_Stream($stream);
53 $logger = new Zend_Log($writer);
55 $logger->info('Informational message');
59 你不能给现有的流资源指定模式.这样作将导致抛出一个<code>Zend_Log_Exception</code>异常.
63 <sect2 id="zend.log.writers.database">
66 <code>Zend_Log_Writer_Db</code>使用<code>Zend_Db</code>写入日志信息到数据库表中.
67 <code>Zend_Log_Writer_Db</code>的构造函数接受一个<code>Zend_Db_Adapter</code>
68 实例,一个表名,和一个数据库字段到事件数据项的映射:
69 <programlisting role="php"><![CDATA[
70 $params = array ('host' => '127.0.0.1',
71 'username' => 'malory',
72 'password' => '******',
73 'dbname' => 'camelot');
74 $db = Zend_Db::factory('PDO_MYSQL', $params);
76 $columnMapping = array('lvl' => 'priority', 'msg' => 'message');
77 $writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping);
79 $logger = new Zend_Log($writer);
81 $logger->info('Informational message');
85 上面的例子写入单个行到名称为<code>log_table_name</code>的数据库表中.数据库字段<code>lvs</code>
86 接收优先级号,名为<code>msg</code>的字段接收日志消息.
91 <xi:include href="Zend_Log-Writers-Firebug.xml">
92 <xi:fallback href="../en/module_specs/Zend_Log-Writers-Firebug.xml"/>
95 <sect2 id="zend.log.writers.null">
96 <title>踩熄Writer</title>
98 <code>Zend_Log_Writer_Null</code>是一个不向任何地方写入任何数据的存根.
101 <programlisting role="php"><![CDATA[
102 $writer = new Zend_Log_Writer_Null;
103 $logger = new Zend_Log($writer);
106 $logger->info('Informational message');
112 <sect2 id="zend.log.writers.mock">
113 <title>测试 Mock</title>
115 <code>Zend_Log_Writer_Mock</code>是一个非常简单的Writer,它纪录所接收到的原始的数据到到作为public属性的
118 <programlisting role="php"><![CDATA[
119 $mock = new Zend_Log_Writer_Mock;
120 $logger = new Zend_Log($mock);
122 $logger->info('Informational message');
124 var_dump($mock->events[0]);
128 // [timestamp] => 2007-04-06T07:16:37-07:00
129 // [message] => Informational message
131 // [priorityName] => INFO
138 清空有mock记录的日志,设置<code>$mock->events = array()</code>即可.
142 <sect2 id="zend.log.writers.compositing">
143 <title>组合Writers</title>
146 没有复合Writer对象,但是一个Log实例可以有任意个Writer.使用<code>addWriter</code>方法
149 <programlisting role="php"><![CDATA[
150 $writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile');
151 $writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile');
153 $logger = new Zend_Log();
154 $logger->addWriter($writer1);
155 $logger->addWriter($writer2);
157 // goes to both writers
158 $logger->info('Informational message');