Symfony2 Monolog Settings for email and file logging

回眸只為那壹抹淺笑 提交于 2019-12-31 23:02:06

问题


I want to setup Symfony2 to send me an email for critical errors, but just log error level errors. Will the following settings do that?

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      grouped
        grouped:
            type: group
            members: [filelog, mail]
        # log all errors to file
        filelog:
            type:         fingers_crossed
            action_level: error
            handler:      nested_stream
        nested_stream:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        # send me an email when we have a critical error
        mail:
            type:         fingers_crossed
            action_level: critical
            handler:      buffered
        buffered:
            type:    buffer
            handler: swift
        swift:
            type:       swift_mailer
            from_email: %mailer_sender%
            to_email:   %error_email%
            subject:    "[FeedStream Error]"
            level:      debug

I saw: http://symfony.com/doc/current/cookbook/logging/monolog_email.html But it doesn't handle error at all, which is a case where I still want logs (but no email). I was pretty sure my config would work, but I don't know enough about the monolog settings. Please let me know if this is correct or if there is a better way.


回答1:


The following is my production monolog config. This is confirmed working sending critical errors, whilst logging 'error' level and above to file. I've also split out the different channels to separate files. The other channels seem to produce errors far less than 'request', so it makes sense to split them out in production for me. Realise that's not your question, but hope it helps someone else; this can pared back to fit most requirements.

monolog:
  handlers:
    main:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_remaining.log"
        channels: ["!doctrine", "!request", "!security"]
    request:
        type: fingers_crossed
        handler: requests
        excluded_404s:
            - ^/phpmyadmin
    requests:
        type:    group
        members: [request_critical, request_error]
    request_critical:
        level: critical
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_request_critical.log"
        channels: [request]
    request_error:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_request.log"
        channels: [request]
    doctrine:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_doctrine.log"
        channels: [doctrine]
    security:
        level: error
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%_security.log"
        channels: [security]
    mail:
        type: fingers_crossed
        action_level: critical
        handler: buffered
    buffered:
        type: buffer
        handler: swift
    swift:
        type: swift_mailer
        from_email: aj.cerqueti@example.com
        to_email:   aj.cerqueti@example.com
        subject:    A critical error occurred


来源:https://stackoverflow.com/questions/10261272/symfony2-monolog-settings-for-email-and-file-logging

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