ZeSoft
ZeSoft

Reputation: 335

log all messages and email only errors with monolog Symfony

i want to log all messages to log file but send email only in case of errors excluding 404. i use this code which works fine, but it does not make sense to me, because this code means that the main handler won't be triggered only if error level is reached then it will be logged or emailed. when in fact all messages still log to the file. what am i missing here?

monolog:
handlers:
    main:
        type:         fingers_crossed
        action_level: error
        excluded_404s:
            - ^/
        handler:      grouped
    grouped:
        type:               group
        members:            [streamed, deduplicated]
    streamed:
        type:               stream
        path:               "%kernel.logs_dir%/%kernel.environment%.log"
        level:              debug
    deduplicated:
        type:               deduplication
        handler:            swift
    swift:
        type:               swift_mailer
        from_email:         %noreply_email%
        to_email:           %webmaster_email%
        subject:            'Error Notification %%message%%'
        level:              error
        formatter:          monolog.formatter.html
        content_type:       text/html
    login:
        type:               stream
        path:               "%kernel.logs_dir%/auth.log"
        level:              info
        channels:           security
    nested:
        type:  stream
        path:  "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
    console:
        type:  console

Edit: here is the final version

monolog:
handlers:
streamed:
    type:               stream
    path:               "%kernel.logs_dir%/%kernel.environment%.log"
    level:              debug
emailed:
        type:         fingers_crossed
        action_level: error
        excluded_404s:
            - ^/
        handler:      swift
swift:
    type:               swift_mailer
    from_email:         %noreply_email%
    to_email:           %webmaster_email%
    subject:            'Error Notification %%message%%'
    level:              error
    formatter:          monolog.formatter.html
    content_type:       text/html
login:
    type:               stream
    path:               "%kernel.logs_dir%/auth.log"
    level:              info
    channels:           security
console:
    type:  console

Upvotes: 0

Views: 2009

Answers (1)

Xymanek
Xymanek

Reputation: 1389

all messages still log to the file. what am i missing here?

The reason it works is that you also have the nested handler which is not actually nested, making it (from MonologBundle point of view) a top-level handler (meaning it will receive all logs generated in your application). Furthermore it points to same file as streamed handler thus making it impossible to distinguish which handler was responsive for a certain logged message.

this code means that the main handler won't be triggered only if error level is reached then it will be logged or emailed

Yes, main handler will collect logs until it gets an error (except for 404s) and then it will flush everything down to grouped handler which in turn pipes everything to streamed and deduplicated handlers

Upvotes: 2

Related Questions