0

我很难理解如何在我的 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将使用该处理程序。我怎样才能做到这一点?

4

1 回答 1

1

您可以为处理程序指定通道。因此,如果您希望“dynamic_request”处理程序仅用于“dynamic_request”日志,您的 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

于 2014-09-17T07:15:53.910 回答