简体   繁体   English

Symfony 2中默认monolog的自定义monolog处理程序

[英]Custom monolog handler for default monolog in Symfony 2

I want to add a custom handler to a default monolog in Symfony 2. 我想将自定义处理程序添加到Symfony 2中的默认monolog。

In my config.yaml file, I have: 在我的config.yaml文件中,我有:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
        myHandler:
            type:  Acme\MyBundle\Monolog\MyCustomHandler
            level: error

My class looks like below: 我的课程如下:

// Acme\MyBundle\Monolog\MyCustomHandler
use Monolog\Logger;
use Monolog\Handler\SocketHandler;
use Monolog\Formatter\LineFormatter;

class MyCustomHandler extends AbstractProcessingHandler
{
    ...
}

But even before I fill my class in I get an error: 但即使在我填写课程之前,我也会收到错误:

invalid handler type "acme\\mybundle\\monolog\\mycustomhandler" given for handler "myHandler" 为处理程序“myHandler”指定的无效处理程序类型“acme \\ mybundle \\ monolog \\ mycustomhandler”

How do I add a custom handler to the default monolog without creating a new monolog service? 如何在不创建新的monolog服务的情况下将自定义处理程序添加到默认monolog?

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部分。

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. 我希望它对你有所帮助。

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. 我刚刚发现Monolog附带了一组各种处理程序,所以你可能想要使用其中一种而不是自己编写。 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 我正在使用LogEntriesHandler登录到logentries.com,但还有一些内容如下所示: https//github.com/Seldaek/monolog#log-specific-servers-and-networked-logging

My Symfony2 config for that looks like that: 我的Symfony2配置看起来像这样:

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%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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