如何在电子邮件主题中包含日志的严重性?

时间:2012-09-18 16:40:15

标签: symfony monolog

我正在使用Monolog和Symfony2并配置了一个记录环境,其中所有内容都记录到文件中,超过一定的阈值通过电子邮件发送给我。我的配置如下。

但是,我无法调整电子邮件主题,以便根据日志的实际级别进行更改。响应时间的差异,例如警告和关键时刻,可能会有所不同。有没有办法做到这一点?

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
        mail:
            type:         fingers_crossed
            action_level: %logger_level%
            handler:      buffered
        buffered:
            type:    buffer
            handler: swift
        swift:
            type:       swift_mailer
            from_email: %logger_from_email%
            to_email:   %logger_to_email%
            subject:    Log  # I want the subject to include the log level somehow
            level:      debug

2 个答案:

答案 0 :(得分:0)

您可以定义两个不同的处理程序(或处理程序链):一个由低级问题激活,另一个由更严重的问题激活。他们发送的电子邮件的主题不同。类似的东西:

monolog:
    handlers:
        mail_critical:
            type:         fingers_crossed
            action_level: critical
            handler:      buffered_critical
            bubble: false
        buffered_critical:
            type:    buffer
            handler: swift_critical
        swift_critical:
            type:       swift_mailer
            from_email: %logger_from_email%
            to_email:   %logger_to_email%
            subject:    CALL 911, YOUR SITE IS BURNING
            level:      critical
        mail_debug:
            type:         fingers_crossed
            action_level: debug
            handler:      buffered_debug
        buffered_debug:
            type:    buffer
            handler: swift_debug
        swift_debug:
            type:       swift_mailer
            from_email: %logger_from_email%
            to_email:   %logger_to_email%
            subject:    Just a normal error
            level:      debug

答案 1 :(得分:0)

您可以在主题中加入 %% level %% 。它将添加 int

或者您可以添加 %% level_name %% ,它将添加腿部级别名称的 string

        swift:
        type:       swift_mailer
        from_email: '%env(ADMIN_EMAIL)%'
        to_email:   '%env(ADMIN_EMAIL)%'
        subject:    '[%kernel.environment%] [LEVEL CODE:%%level%%][LEVEL NAME:%%level_name%%] Some subject text! %%message%%'
        level:      error
        formatter:  monolog.formatter.html
        content_type: text/html

更多https://github.com/Seldaek/monolog/blob/master/doc/message-structure.md