Search code examples
symfonymonolog

Symfony2 + Monolog: Different actions for different error levels


I have set up Monolog for our Symfony2 project to email out critical errors to us when they occur.

However I would also like to log non-critical errors also, and to email these errors out to different recipients. I am struggling to see this in the documentation however it looks like it should be possible. I have set up the config as follows:

parameters:
    error_mail_sender: [email protected]
    error_mail_recipients: [[email protected]]
    critical_error_mail_recipients: [[email protected], [email protected]]

monolog:
    handlers:
        main_critical:
            type:         fingers_crossed
            action_level: critical
            handler:      grouped_critical
            bubble:       false
        main_error:
            type:         fingers_crossed
            action_level: error
            handler:      grouped_error
        grouped_critical:
            type:    group
            members: [streamed, buffered_critical]
        grouped_error:
            type:    group
            members: [streamed, buffered_error]
        streamed:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        buffered_critical:
            type:    buffer
            handler: swift_critical
        buffered_error:
            type:    buffer
            handler: swift_error
        swift_critical:
            type:       swift_mailer
            from_email: %error_mail_sender%
            to_email:   %error_mail_recipients%
            subject:    Critical error occurred!
            level:      debug
        swift_error:
            type:       swift_mailer
            from_email: %error_mail_sender%
            to_email:   %critical_error_mail_recipients%
            subject:    Non-critical error occurred
            level:      debug

With this set up we receive the critical errors but don't get the non-critical errors.

This set up was loosely based on the (unaccepted) answer to this question: How to include the severity of a log in the e-mail subject?. (I would have up-voted the answer had it worked for me!!)

Can anyone spot what is wrong with this?

Thanks!


Solution

  • The problem seems to be bubble: false in the first handler, that means it'll stop propagating messages to other handlers.

    Another note, I would remove the group and the streamed handler from grouped_critical, because it will already receive errors from grouped_error, resulting in duplicate entries in the log file.

    So this should work:

    parameters:
        error_mail_sender: [email protected]
        error_mail_recipients: [[email protected]]
        critical_error_mail_recipients: [[email protected]]
    
    monolog:
        handlers:
            main_critical:
                type:         fingers_crossed
                action_level: critical
                handler:      buffered_critical
            buffered_critical:
                type:    buffer
                handler: swift_critical
            swift_critical:
                type:       swift_mailer
                from_email: %error_mail_sender%
                to_email:   %error_mail_recipients%
                subject:    Critical error occurred!
                level:      debug
    
            main_error:
                type:         fingers_crossed
                action_level: error
                handler:      grouped_error
            grouped_error:
                type:    group
                members: [streamed, buffered_error]
            streamed:
                type:  stream
                path:  "%kernel.logs_dir%/%kernel.environment%.log"
                level: debug
            buffered_error:
                type:    buffer
                handler: swift_error
            swift_error:
                type:       swift_mailer
                from_email: %error_mail_sender%
                to_email:   %critical_error_mail_recipients%
                subject:    Non-critical error occurred
                level:      debug
    

    I reordered them to make it easier to see the two handler chains as well.