Writers
一个Writer是继承自 写入到流(Streams)
要把日志写入到输出缓冲区,使用URL $writer = new Zend_Log_Writer_Stream('php://output'); $logger = new Zend_Log($writer); $logger->info('Informational message'); 写入日志数据到一个文件,使用» Filesystem URLs之一: $writer = new Zend_Log_Writer_Stream('/path/to/logfile'); $logger = new Zend_Log($writer); $logger->info('Informational message'); "a" )的方式打开.要以不同的模式打开,Zend_Log_Writer_Stream
构造函数接受第二个参数作为可选的模式参数.
$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'); Zend_Log_Exception 异常.
写入到数据库
$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'); log_table_name 的数据库表中.数据库字段lvs
接收优先级号,名为msg 的字段接收日志消息.
踩熄Writer
$writer = new Zend_Log_Writer_Null; $logger = new Zend_Log($writer); // goes nowhere $logger->info('Informational message'); 测试 Mock
$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 // )
清空有mock记录的日志,设置 组合Writers
没有复合Writer对象,但是一个Log实例可以有任意个Writer.使用 $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');
|