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

标签 symfony logging monolog

我正在开发 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/

相关文章:

php - 如何更改 Symfony 2 中的默认 EntityRepository 基类?

php - 将 Symfony 3 中的 Doctrine 连接到 MySQL

python 从内核日志中读取最后三行

mysqld : where is the query log file saved?

php - phpunit 是否使用与 MAMP 中配置的不同的 php.ini 文件?

logging - 有没有一种干净的方法来记录 Groovy 中方法的结果?

php - 获取 Monolog JSON 堆栈跟踪作为数组

Symfony Autowiring 独白 channel

php - 在 php 中使用 monolog

php - PHP String 中的 Twig 标签或函数