我正在开发 Symfony 2 Web 应用程序,我想使用特定 channel 将 Monolog 记录器注入(inject)服务:
配置:
monolog:
handlers:
main:
type: stream
path: %kernel.root_dir%/%kernel.environment%.log
level: error
#channels: [!alert]
alert:
type: stream
path: %kernel.root_dir%/%kernel.environment%.alert.log
level: info
channels: [alert]
服务配置:
services:
some_service:
class: Some\Service
arguments: [@logger]
tags:
- { name: monolog.logger, channel: alert }
服务:
class SomeService {
protected $logger;
public function __construct($logger) {
$this->logger = $logger;
$this->logger->info('Log this!');
}
prod.log 文件:
[2016-03-28 11:25:47] alert.INFO: Log this!
问题:虽然我专门使用
alert
注入(inject)记录器 channel ,消息由 main
处理处理程序。因此,消息被记录到 prod.log
文件而不是 prod.alert.log
文件。当我离开线路
channels: [!alert]
作为评论,该消息被记录到 prod.log
.当我通过删除注释激活此行时,根本不会记录消息(主处理程序正确忽略了 channel )。为了使用特定的处理程序来定位特定的日志文件、邮件程序等,我需要做什么?给
alert
的消息 channel 应由 alert
处理处理程序,而所有其他处理程序都被忽略。
最佳答案
使用为 Monolog
创建的特殊服务处理程序:
services:
some_service:
class: Namespace\Some\Service
arguments: ["@monolog.logger.alert"]
关于Symfony 2 : Injecting logger for specific channel/handler to services,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261717/