How to include the severity of a log in the e-mail subject?

旧城冷巷雨未停 提交于 2019-12-11 02:19:56

问题


I'm using Monolog with Symfony2 and have configured a logging environment where everything is logged to file, and above a certain threshold are e-mailed to me. My config is below.

However, I have not been able to adjust the e-mail subject so that it changes based on the actual level of the log. The difference in response time, for say, a warning and a critical, would probably be different. Is there a way to do this?

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

回答1:


You can define two different handlers (or handler chains): one is activated by low level problems and the other by more serious issues. They have different subjects for the email they send. Something like:

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



回答2:


You can include %%level%% in the subject. It will add int.

Or you can add %%level_name%% it will add string of leg level name.

        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

More can be found here https://github.com/Seldaek/monolog/blob/master/doc/message-structure.md



来源:https://stackoverflow.com/questions/12481353/how-to-include-the-severity-of-a-log-in-the-e-mail-subject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!