跳到内容

如何配置 Monolog 以通过电子邮件发送错误

编辑此页

3.6

MonologBundle 3.6 中添加了使用 Symfony mailer 发送电子邮件错误的支持。

Monolog 可以配置为在应用程序内发生错误时发送电子邮件。 此配置需要几个嵌套的处理程序,以避免收到过多的电子邮件。 此配置乍一看很复杂,但当分解时,每个处理程序都相当简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# config/packages/prod/monolog.yaml
monolog:
    handlers:
        main:
            type:         fingers_crossed
            # 500 errors are logged at the critical level
            action_level: critical
            # to also log 400 level errors (but not 404's):
            # action_level: error
            # excluded_http_codes: [404]
            handler:      deduplicated
        deduplicated:
            type:    deduplication
            handler: symfony_mailer
        symfony_mailer:
            type:       symfony_mailer
            from_email: '[email protected]'
            to_email:   '[email protected]'
            # or list of recipients
            # to_email:   ['[email protected]', '[email protected]', ...]
            subject:    'An Error Occurred! %%message%%'
            level:      debug
            formatter:  monolog.formatter.html
            content_type: text/html

main 处理程序是一个 fingers_crossed 处理程序,这意味着它仅在达到操作级别时触发,在本例中为 criticalcritical 级别仅针对 5xx HTTP 代码错误触发。 如果此级别被触发一次,fingers_crossed 处理程序将记录所有消息,而不管其级别如何。 handler 设置意味着输出随后传递到 deduplicated 处理程序。

提示

如果您希望 400 级别和 500 级别错误都触发电子邮件,请将 action_level 设置为 error 而不是 critical。 有关示例,请参见上面的代码。

deduplicated 处理程序保留请求的所有消息,然后一次性将它们传递到嵌套处理程序,但前提是记录在给定的时间段(默认为 60 秒)内是唯一的。 重复的记录将被丢弃。 添加此处理程序可以将通知数量减少到可管理的水平,尤其是在严重的故障情况下。 您可以使用 time 选项调整时间段

1
2
3
4
5
6
7
8
9
# config/packages/prod/monolog.yaml
monolog:
    handlers:
        # ...
        deduplicated:
            type: deduplication
            # the time in seconds during which duplicate entries are discarded (default: 60)
            time: 10
            handler: symfony_mailer

然后,消息将传递到 symfony_mailer 处理程序。 这是实际处理通过电子邮件向您发送错误的处理程序。 此设置非常简单,包括收件人和发件人地址、格式化程序、内容类型和主题。

您可以将这些处理程序与其他处理程序组合,以便错误仍然记录在服务器上,并且发送电子邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# config/packages/prod/monolog.yaml
monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: critical
            handler:      grouped
        grouped:
            type:    group
            members: [streamed, deduplicated]
        streamed:
            type:  stream
            path:  '%kernel.logs_dir%/%kernel.environment%.log'
            level: debug
        deduplicated:
            type:    deduplication
            handler: symfony_mailer
        symfony_mailer:
            type:         symfony_mailer
            from_email:   '[email protected]'
            to_email:     '[email protected]'
            subject:      'An Error Occurred! %%message%%'
            level:        debug
            formatter:    monolog.formatter.html
            content_type: text/html

这使用 grouped 处理程序将消息发送到两个组成员,即 deduplicatedstream 处理程序。 现在,消息将被写入日志文件并通过电子邮件发送。

这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本