PHP错误报告生产环境与开发环境的区别

13

在开发和生产应用程序中设置错误报告的最佳实践是什么?目前我有以下内容:

// development
error_reporting(E_ALL);

// production
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ERROR | E_WARNING | E_PARSE);

我的观点是,在开发过程中应该尽量避免出现错误,并且在生产环境中将 error_reporting 设置为关闭,除非你需要进行调试。 - Can O' Spam
3
在生产环境中禁用 display_errors - Spoody
我已更新我的问题以确认在实时环境中显示错误已关闭并启用了日志错误。 - xylar
在任何环境中都最好完全关闭display_errors。有时候错误会导致头部信息过早发送,从而彻底破坏你的脚本!要跟踪日志! - delboy1978uk
2个回答

26

引用应该随PHP捆绑在一起的 php-production.ini

; PHP comes packaged with two INI files. One that is recommended to be used
; in production environments and one that is recommended to be used in
; development environments.

; php.ini-production contains settings which hold security, performance and
; best practices at its core. But please be aware, these settings may break
; compatibility with older or less security conscience applications. We
; recommending using the production ini in production and testing environments.
; display_errors
;   Default Value: On
;   Development Value: On
;   Production Value: Off

; display_startup_errors
;   Default Value: Off
;   Development Value: On
;   Production Value: Off

; error_reporting
;   Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
;   Development Value: E_ALL
;   Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT

; html_errors
;   Default Value: On
;   Development Value: On
;   Production value: On

; log_errors
;   Default Value: Off
;   Development Value: On
;   Production Value: On

既然你要求最佳实践,我建议你采用这种做法。


3
为了获得最佳的错误日志记录体验,请将 error_reporting 设置为 -1(即全部),关闭 display_errors ,并设置自定义的 error_log
然后在终端中输入 tail -f /path/to/error_log。现在,您的通知、警告和错误将实时滚动显示,而不会扭曲网页的显示。
始终记录所有内容都是值得的。在任何环境下都是如此。

1
我不得不处理一个网站,它有很多通知错误,以至于很难发现/真正的/错误。此外,由于某种原因,一个请求在记录所有错误时会抛出500错误。 - xylar
大多数通知只需要简单的修复,比如使用 isset() 检查。在循环中修复一个通知可以大大减少错误日志。所有东西都是错误!摆脱它们!一个空日志是一个按照预期运行的网站! - delboy1978uk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接