Custom monolog handler for default monolog in Symfony 2

后端 未结 2 1534
难免孤独
难免孤独 2020-12-14 17:41

I want to add a custom handler to a default monolog in Symfony 2.

In my config.yaml file, I have:

monolog:
    handlers:
        main:
          


        
相关标签:
2条回答
  • 2020-12-14 18:24

    Try this:

    monolog:
        handlers:
            main:
                type:  stream
                path:  %kernel.logs_dir%/%kernel.environment%.log
                level: debug
            custom:
                type: service
                id: my_custom_handler
    
    services:
        my_custom_handler:
            class: Acme\MyBundle\Monolog\MyCustomHandler
    

    If you want to use it as default handler then you should change a bit monolog section I wrote above.

    monolog:
        handlers:
            main:
                type:  stream
                path:  %kernel.logs_dir%/%kernel.environment%.log
                level: debug
                handler: custom
            custom:
                type: service
                id: my_custom_handler
    

    I hope it helps you.

    0 讨论(0)
  • 2020-12-14 18:33

    I just found out that Monolog ships with a set of various handlers so you might wanna use one of those instead of writing your own. I am using the LogEntriesHandler for logging to logentries.com but there are a few more as documented here: https://github.com/Seldaek/monolog#log-specific-servers-and-networked-logging

    My Symfony2 config for that looks like that:

    monolog:
        main:
            type:  fingers_crossed
            level: debug
            handler: nested
        custom:
            type: service
            id: monolog.handler.logentries
            level: error
    
    services:
        monolog.handler.logentries:
            class: Monolog\Handler\LogEntriesHandler
            arguments:
                token: %logentries_token%
    
    0 讨论(0)
提交回复
热议问题