Zend_Log在application.ini中有多个编写器

时间:2011-09-07 14:20:52

标签: zend-framework logging zend-log

我的配置中有一个记录器,如下所示:

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/err.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 3

但我想将它用于错误日志。对于另一个日志想要使用另一个日志文件。出于这个原因,我将这些行添加到app.ini

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream[] = APPLICATION_PATH "/../logs/debug.log"
resources.log.stream.writerParams.stream[] = APPLICATION_PATH "/../logs/info.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority[] = 7
resources.log.stream.filterParams.priority[] = 5

但是这不起作用我想使用differetn文件用于不同的优先级。但是记录器会覆盖第一个记录器。怎么样?

2 个答案:

答案 0 :(得分:8)

application.ini示例:

resources.log.err.writerName = "Stream"
resources.log.err.writerParams.stream =  APPLICATION_PATH "/../data/logs/ERR.log"
;resources.log.stream.formatterParams.format = "%priority%:%message% %timestamp% %priorityName%  %info% PHP_EOL"
resources.log.err.filterName = "Priority"
resources.log.err.filterParams.priority = 3
resources.log.err.filterParams.operator = "=="

resources.log.warn.writerName = "Stream"
resources.log.warn.writerParams.stream = APPLICATION_PATH "/../data/logs/WARN.log"
;resources.log.warn.formatterParams.format = "%priority%:%message%:%ip%:%userid% %timestamp% %priorityName%  %info% PHP_EOL"
resources.log.warn.filterName = "Priority"
resources.log.warn.filterParams.priority = 4
resources.log.warn.filterParams.operator = "=="

在bootstrap中:

protected function _initLog() {
    $options = $this->getOption('resources');
    $partitionConfig = $this->getOption('log');
    $logOptions = $options['log'];
    $logger = Zend_Log::factory($logOptions);
    $logger->addPriority('USERACTION', 8);
    $logger->addPriority('DBLOG', 9);
    Zend_Registry::set('logger', $logger);
}

然后在代码中:

$this->logger = Zend_Registry::get('logger');
$this->logger->setEventItem('ip', $_SERVER['REMOTE_ADDR']);
$this->logger->setEventItem('userid', $this->userId);

使用这种方式:

$this->logger->log('Test error', Zend_Log::WARN);

或者这样:

$this->logger->warn('Test error');

或者这样:

$this->logger->log('Test error', 4);

答案 1 :(得分:1)

您可以使用过滤器来完成此任务。只需让更高优先级的事件通过过滤器,直到您想要记录它们。

http://framework.zend.com/manual/en/zend.log.filters.html

你的设置有什么不对,你试图使用你不应该使用的同一个作家。使用过滤器选择编写器并将某些日志文件与某些编写器关联,如示例的第二部分所示。

我觉得这样的事情可以解决问题:

resources.log.stream.writerName = "myDebugWriter"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/debug.log"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::WARN
resources.log.stream.writerName = "myInfoWriter"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/info.log"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::INFO