如何仅发送PHP致命错误? [英] How to email PHP Fatal Errors only?
问题描述
我要写这个功能让我首先专注于致命的错误,然后我将把它打开到不太紧急的警告和通知。我发现这个代码,但它发送一个小小的警告通知和错误。
http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/
如何修改此代码,以便只处理以下事情:
- 致命语法错误
- 未定义的功能
- 无法包含文件
set_error_handler
允许您指定用户定义的错误处理功能,以决定如何处理某些(但不是非致命的)错误。然后,您可以以您认为必要的任何方式处理特定类型的错误,例如通过电子邮件通知系统管理员,或将其保存到特定的日志文件。有关详细信息,请参阅: PHP Doc 。
关于您的请求,您可以采用以下方法:
函数myErrorHandler($ errno,$ errstr,$ errfile,$ errline)
{
if(!(error_reporting()& $ errno)){
//不包括此错误代码在error_reporting
return;
}
switch($ errno){
case E_USER_ERROR:
case E_ERROR:
case E_COMPILE_ERROR:
echo< b>我的错误< / b> [$ errno] $ errstr< br /> \\\
;
echo文件$ errfile中的$ errline上的致命错误;
echo,PHP。 PHP_VERSION。 (。PHP_OS)< br /> \\\
;
echoAborting ...< br /> \\\
;
emailErrorFunction($ errno,$ erstr,$ errfile,$ errline);
exit(1);
break;
默认值:
echo未知错误类型:[$ errno] $ errstr< br /> \\\
;
break;
}
/ *不执行PHP内部错误处理程序* /
return true;
}
//报告所有错误
error_reporting(E_ALL);
//用户自定义错误处理程序打印输出
set_error_handler('myErrorHandler');
由于错误处理不适用于致命错误(解析错误,未定义的功能等)您需要使用 register_shutdown_function
将其粘贴,如相关问题:
I am in the middle of refining a rather large bit of code. Alot of it contains many general warnings and notices which do not affect the execution of the code (ie: undefined varilables, or array keys without qoutes).
I want to write a function which allows me to concentrate on the fatal errors first and then I will open it up to the less urgent warnings and notices. I found this code, but it emails every little warning notice and error.
http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/
How can I modify this code so it only deals with things like:
- fatal syntax errors
- undefined functions
- failure to include files
set_error_handler
allows you to specify a user-defined error handling function for deciding what to do with certain (but only non-fatal) errors. You can then handle specific types of errors in any fashion you deem necessary, for example notifying a system administrator via email, or saving to a specific log file. See: PHP Doc for further details.
With regards to your request you could the following approach:
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
case E_ERROR:
case E_COMPILE_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
emailErrorFunction($errno,$erstr,$errfile,$errline);
exit(1);
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// Report all errors
error_reporting(E_ALL);
// User a custom error handler to print output
set_error_handler( 'myErrorHandler' );
As that error-handling does not work for Fatal Errors (Parse errors, undefined functions etc.), you need to tape this with register_shutdown_function
as outlined in a related question:
这篇关于如何仅发送PHP致命错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!