Symfony Monolog生产模式下无法发送错误邮件问题
问题
Symfony生产模式下,未定义变量这类错误无法触发邮件通知;切换到开发模式时邮件可正常发送,仅生产模式存在该异常。当前monolog.yaml的生产环境配置如下:
when@prod: monolog: handlers: main: type: fingers_crossed action_level: error handler: deduplicated excluded_http_codes: [ 404, 405 ] deduplicated: type: deduplication handler: symfony_mailer symfony_mailer: type: symfony_mailer from_email: '%env(APP_MAIL_SENDER)%' to_email: '%env(DEV_EMAIL)%' subject: 'An Error Occurred! %%message%%' level: debug formatter: monolog.formatter.html content_type: text/html
解决方案
核心原因是:
- Symfony生产模式下,未定义变量这类PHP
E_NOTICE级别的错误,默认会被记录为notice级别的日志,而非error级别; - 当前配置中
fingers_crossed处理器的action_level设为error,只有达到error及以上级别的日志才会触发后续的邮件通知流程。
针对问题有两种可行修改方式:
方式一:调整Monolog的触发级别
直接修改fingers_crossed处理器的action_level为notice,让notice级别的日志也能触发邮件通知:
when@prod: monolog: handlers: main: type: fingers_crossed action_level: notice # 从error改为notice handler: deduplicated excluded_http_codes: [ 404, 405 ] # 其余配置保持不变
方式二:提升PHP错误报告级别(可选)
如果希望把notice级别的错误直接转为error级别,可以在生产环境配置中调整PHP错误报告规则:
在public/index.php(或bin/console)开头添加以下代码:
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT); ini_set('display_errors', 0);
或者在php.ini中设置:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
这种方式会让未定义变量这类错误被视为error级别,无需修改Monolog的action_level即可触发邮件。
内容的提问来源于stack exchange,提问作者Luu
相关产品推荐
相关产品推荐

