简体   繁体   中英

only log certain logs with monolog using a channel in symfony

I'm having a hard time understanding how to configure logging in my symfony app to do what i need to. I don't understand the difference/relationship between handlers and channels. I have the following in my 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

I then have this defined in my 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 }

The desired behavior is for only error logs to be logged to %kernel.logs_dir%/%kernel.environment%.log and then i would like to inject a logger into my DynamicResponseProvider that logs to %kernel.logs_dir%/%kernel.environment%_dynamic.log . The documentation is confusing me. It states the following:

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

Does this mean that because i have the dynamic_request handler defined any debug logs will be logged here? That is not my desired behavior. What i would like is that ONLY the DynamicResponseProvider will use that handler. How can i achieve this?

You can specify a channel for a handler. So, if you want that the 'dynamic_request' handler is used only for 'dynamic_request' logs, your config.yml must be :

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

See http://symfony.com/doc/current/cookbook/logging/channels_handlers.html for more example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM