管理PHP错误 [英] Managing PHP Errors
问题描述
我一直在网上搜索,尝试找到一种方式来捕捉PHP所引发的所有错误(5.3)
我一直在阅读文档和它看起来像 set_error_handler
是我需要的,但它不会导致致命/解析错误。我不知道这是否可以...
这是我的来源: https://github.com/tarnfeld/PHP-Error-Handler
如果您知道更好的解决方案,请随时进行fork / commit。
更新!
使用下面的答案完成了一个错误处理程序的编写,它处理E_ERROR | E_PARSE | E_WARNING | E_NOTICE并且会在死机时杀死脚本! : - )
引用手册:
以下错误类型无法用用户定义的函数处理:
E_ERROR
,E_PARSE
,E_CORE_ERROR
,E_CORE_WARNING
,E_COMPILE_ERROR
,E_COMPILE_WARNING
,并且在set_error_handler()
被调用的文件中引发的E_STRICT
的大部分。
这意味着不能被抓住和处理。你可以做的是设置一个额外的处理程序,以便在脚本退出时运行。 register_shutdown_function
。
在该处理程序中,使用 error_get_last
,并检查是否是致命错误。这不会允许您继续执行脚本执行。脚本将会结束,但您可以进行任何清理或记录(通常会将错误记录到error.log中)或其他任何内容。
有一个以下注释中的用户提供的示例 set_error_handler
register_shutdown_function('shutdownFunction');
pre>
function shutDownFunction(){
$ error = error_get_last();
if($ error ['type'] == 1){
//做你的东西
}
}
但请注意,这仍然只会捕获某些额外的运行时错误。
I have been searching all over the net to try and find a way to catch all errors thrown by PHP (5.3)
I have been reading through the documentation and it looks like
set_error_handler
is what I need but it doesn't get fatal/parse errors. I'm not sure if this is possible...Here is my source: https://github.com/tarnfeld/PHP-Error-Handler Feel free to fork/commit if you know better solutions to all of this.
Thanks in advanced!
Updated!
Using the answers below I finished writing an error handler, it takes care of E_ERROR|E_PARSE|E_WARNING|E_NOTICE and will kill the script when it is fatal! :-)
解决方案Quoting the manual:
The following error types cannot be handled with a user defined function:
E_ERROR
,E_PARSE
,E_CORE_ERROR
,E_CORE_WARNING
,E_COMPILE_ERROR
,E_COMPILE_WARNING
, and most ofE_STRICT
raised in the file whereset_error_handler()
is called.This means Fatals cannot be caught and handled. What you can do is setup an additional handler to be run when the script exits, e.g.
register_shutdown_function
.In that handler, use
error_get_last
and check if it was a Fatal Error. This will not allow you to continue script execution though. The script will end, but you can do any cleanup or logging (usually fatals will be logged anyway to the error.log) or whatever.There is a user-supplied example in the comments below
set_error_handler
register_shutdown_function('shutdownFunction'); function shutDownFunction() { $error = error_get_last(); if ($error['type'] == 1) { //do your stuff } }
But note that this still will only catch certain additional runtime errors.
这篇关于管理PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!