只使用symfony中的通道使用monlog记录某些日志


only log certain logs with monolog using a channel in symfony

我很难理解如何在我的symfony应用程序中配置日志以完成我需要的操作。我不理解处理程序和通道之间的区别/关系。我的config.yml 中有以下内容

monolog:
    handlers:
        main:
            type: fingers_crossed
            action_level: error
            handler: nested
        nested:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
        dynamic_request:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%_dynamic.log"
            level: debug

然后我在我的services.yml 中定义了这一点

jsonstub.dynamic.response_provider:
    class: ProgrammingAreHard'JsonStub'CoreBundle'Domain'Dynamic'EventListener'DynamicResponseProvider
    arguments:
        - @security.context
        - @logger
        - %kernel.environment%
    tags:
        - { name: kernel.event_listener, event: security.interactive_login, method: onAuthentication }
        - { name: monolog.logger, channel: dynamic_request }

所需的行为是只将error日志记录到%kernel.logs_dir%/%kernel.environment%.log,然后我想在我的DynamicResponseProvider中注入一个记录到%kernel.logs_dir%/%kernel.environment%_dynamic.log的记录器。文件让我很困惑。它说明了以下内容:

configuration defines a stack of handlers which will be called in the order where they are defined

这是否意味着因为我定义了dynamic_request处理程序,所以任何调试日志都将记录在这里?这不是我想要的行为。我希望只有DynamicResponseProvider会使用该处理程序。我怎样才能做到这一点?

您可以为处理程序指定通道。因此,如果您希望"dynamic_request"处理程序仅用于"dynamic_rerequest"日志,那么您的config.yml必须是:

  dynamic_request:
        type:  stream
        path:  "%kernel.logs_dir%/%kernel.environment%_dynamic.log"
        level: debug
        channels: dynamic_request

请参阅http://symfony.com/doc/current/cookbook/logging/channels_handlers.html例如