Formatters
Formatter是一个负责取得一个描述日志事件的 有些Writer不是面向行的并且不能使用Formatter.一个例子是数据库Writer,它向数据库中直接插入一个事件项. 对于这样的Writer不支持Formatter,如果你对这样的Writer设置一个Formatter将抛出一个异常. 简单格式化
<?php $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL; $formatter = new Zend_Log_Formatter_Simple($format);
使用Writer的 <?php $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"
格式化到XML
<?php $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'); 上面的代码输出下面的XML(为清晰添加了空格): <logEntry> <timestamp>2007-04-06T07:24:37-07:00</timestamp> <message>informational message</message> <priority>6</priority> <priorityName>INFO</priorityName> </logEntry>
可以定制根元素并指定一个到事件数据数组的XML元素映射.
<?php $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'); logEntry 为log .
还可以映射元素msg 到事件数据项message ,下面是输出结果:
<log> <msg>informational message</msg> <level>INFO</level> </log>
|