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

时间:2022-10-16 15:08:15

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?

2 个解决方案

#1


32  

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.

我希望它对你有所帮助。

#2


7  

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

我刚刚发现Monolog附带了一组各种处理程序,所以你可能想要使用其中一种而不是自己编写。我正在使用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%

#1


32  

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.

我希望它对你有所帮助。

#2


7  

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

我刚刚发现Monolog附带了一组各种处理程序,所以你可能想要使用其中一种而不是自己编写。我正在使用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%