Symfony 2:将特定 channel/handler 的记录器注入服务

Symfony 2: Injecting logger for specific channel/handler to services

我正在开发 Symfony 2 网络应用程序,我想使用特定通道向服务注入 Monolog 记录器:

配置:

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 通道注入记录器,但消息是由 main 处理程序处理的。因此,消息被记录到 prod.log 文件而不是 prod.alert.log 文件中。

当我将 channels: [!alert] 行作为评论时,消息被记录到 prod.log。当我通过删除评论激活此行时,根本不会记录消息(主处理程序正确地忽略了频道)。

为了使用特定的处理程序来定位特定的日志文件、邮件程序等,我需要做什么? alert 频道的消息应该由 alert 处理程序处理,而忽略所有其他处理程序。

使用为 Monolog 处理程序创建的特殊服务:

services:
    some_service:
        class: Namespace\Some\Service
        arguments: ["@monolog.logger.alert"]