Symfony2 Monolog用于电子邮件和文件记录的设置

时间:2012-04-21 17:21:01

标签: symfony monolog

我想设置Symfony2向我发送critical错误的电子邮件,但只记录error级错误。以下设置会这样做吗?

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      grouped
        grouped:
            type: group
            members: [filelog, mail]
        # log all errors to file
        filelog:
            type:         fingers_crossed
            action_level: error
            handler:      nested_stream
        nested_stream:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        # send me an email when we have a critical error
        mail:
            type:         fingers_crossed
            action_level: critical
            handler:      buffered
        buffered:
            type:    buffer
            handler: swift
        swift:
            type:       swift_mailer
            from_email: %mailer_sender%
            to_email:   %error_email%
            subject:    "[FeedStream Error]"
            level:      debug

我看到:http://symfony.com/doc/current/cookbook/logging/monolog_email.html但它根本不处理error,这是我仍然需要日志(但没有电子邮件)的情况。我很确定我的配置能够正常工作,但我对monolog设置还不太了解。如果这是正确的或者有更好的方法,请告诉我。

1 个答案:

答案 0 :(得分:7)

以下是我的制作monolog配置。这已确认正在发送严重错误,同时将“错误”级别及以上级别记录到文件中。我还拆分了不同的频道来分隔文件。其他渠道似乎产生的错误远远少于“请求”,因此将它们分解为生产对我来说是有意义的。意识到这不是你的问题,但希望它可以帮助别人;这可以减少以满足大多数要求。

monolog:
  handlers:
    main:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_remaining.log"
        channels: ["!doctrine", "!request", "!security"]
    request:
        type: fingers_crossed
        handler: requests
        excluded_404s:
            - ^/phpmyadmin
    requests:
        type:    group
        members: [request_critical, request_error]
    request_critical:
        level: critical
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_request_critical.log"
        channels: [request]
    request_error:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_request.log"
        channels: [request]
    doctrine:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_doctrine.log"
        channels: [doctrine]
    security:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_security.log"
        channels: [security]
    mail:
        type: fingers_crossed
        action_level: critical
        handler: buffered
    buffered:
        type: buffer
        handler: swift
    swift:
        type: swift_mailer
        from_email: aj.cerqueti@example.com
        to_email:   aj.cerqueti@example.com
        subject:    A critical error occurred