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

Tro*_*ike 20 php symfony monolog

我想将自定义处理程序添加到Symfony 2中的默认monolog .

在我的config.yaml档案中,我有:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
        myHandler:
            type:  Acme\MyBundle\Monolog\MyCustomHandler
            level: error
Run Code Online (Sandbox Code Playgroud)

我的课程如下:

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

class MyCustomHandler extends AbstractProcessingHandler
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

但即使在我填写课程之前,我也会收到错误:

为处理程序"myHandler"指定的无效处理程序类型"acme\mybundle\monolog\mycustomhandler"

如何在不创建新的monolog服务的情况下将自定义处理程序添加到默认monolog?

chm*_*iuk 35

试试这个:

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
Run Code Online (Sandbox Code Playgroud)

如果你想将它用作默认处理程序,那么你应该改变我上面写的一些monolog部分.

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            handler: custom
        custom:
            type: service
            id: my_custom_handler
Run Code Online (Sandbox Code Playgroud)

我希望它对你有所帮助.


tot*_*tas 7

我刚发现Monolog船上有各种各样的处理程序,所以你可能想要使用其中一种而不是自己编写.我正在使用LogEntriesHandlerfor log to logentries.com,但是还有一些如此处记录的:https://github.com/Seldaek/monolog#log-specific-servers-and-networked-logging

我的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%
Run Code Online (Sandbox Code Playgroud)