0

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
ZeSoft
  • 305
  • 2
  • 4
  • 13

1 Answers1

2

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

Xymanek
  • 1,357
  • 14
  • 25
  • Thanks Xymanek now i understand, so to log everything to log and only email errors excluding 404. i will edit my question to include my final code, please see if it's correct. – ZeSoft Aug 28 '17 at 20:50
  • @ZeSoft You still need to wrap `deduplicated` handler inside a `fingers_crossed` one. `excluded_404s` option works only on fingers crossed handler type – Xymanek Aug 30 '17 at 06:24
  • thank you @Xymanek for your answer so i edited my code above and i removed the deduplicated handler and directly assigned swift handler to emailed handler, is it good now? and what does the type deduplication mean? – ZeSoft Aug 30 '17 at 09:53
  • @ZeSoft check monolog's docs: https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#wrappers--special-handlers – Xymanek Aug 30 '17 at 11:02