【问题标题】:log all messages and email only errors with monolog Symfony使用独白 Symfony 记录所有消息和仅电子邮件错误
【发布时间】:2017-08-28 12:12:29
【问题描述】:

我想将所有消息记录到日志文件,但仅在出现错误时发送电子邮件,不包括 404。 我使用的代码可以正常工作,但对我来说没有意义,因为此代码意味着只有在达到错误级别时才会触发主处理程序,然后它将被记录或通过电子邮件发送。事实上,所有消息仍然记录到文件中。我在这里错过了什么?

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

编辑:这是最终版本

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

【问题讨论】:

    标签: symfony monolog


    【解决方案1】:

    所有消息仍会记录到文件中。我在这里错过了什么?

    它起作用的原因是您还有 nested 处理程序,它实际上没有嵌套,使其(从 MonologBu​​ndle 的角度)成为顶级处理程序(意味着它将接收您的应用程序中生成的所有日志)。此外,它指向与streamed 处理程序相同的文件,因此无法区分哪个处理程序响应了某个记录的消息。

    此代码意味着只有在达到错误级别时才会触发主处理程序,然后将其记录或通过电子邮件发送

    是的,main 处理程序会收集日志,直到出现错误(404 秒除外),然后它会将所有内容刷新grouped 处理程序,然后将所有内容通过管道传送到streameddeduplicated 处理程序

    【讨论】:

    • 谢谢 Xymanek 现在我明白了,所以要记录所有要记录的内容,并且只记录除 404 之外的电子邮件错误。我将编辑我的问题以包含我的最终代码,请查看它是否正确。
    • @ZeSoft 您仍然需要将deduplicated 处理程序包装在fingers_crossed 中。 excluded_404s 选项仅适用于手指交叉的处理程序类型
    • 感谢@Xymanek 的回答,所以我在上面编辑了我的代码,并删除了去重处理程序并将快速处理程序直接分配给通过电子邮件发送的处理程序,现在好吗?类型重复数据删除是什么意思?
    • @ZeSoft 查看独白的文档:github.com/Seldaek/monolog/blob/master/doc/…
    猜你喜欢
    • 2022-09-27
    • 2012-03-17
    • 2012-04-23
    • 2014-05-15
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    相关资源
    最近更新 更多