如何仅通过电子邮件发送PHP致命错误? 我正在提炼一个相当大的代码。其中许多包含许多一般性警告和注意事项,这些警告和注意事项不会影响代码的执行(即:未定义的变量,或不带qoutes的数组键)
我想写一个函数,让我能够集中精力在致命的错误,然后我会打开它,以较少的紧急警告和通知。我发现了这段代码,但它会给每一个小的警告和错误发送电子邮件 如何修改此代码,使其只处理以下事项:如何仅通过电子邮件发送PHP致命错误? 我正在提炼一个相当大的代码。其中许多包含许多一般性警告和注意事项,这些警告和注意事项不会影响代码的执行(即:未定义的变量,或不带qoutes的数组键),php,syntax,error-handling,Php,Syntax,Error Handling,我想写一个函数,让我能够集中精力在致命的错误,然后我会打开它,以较少的紧急警告和通知。我发现了这段代码,但它会给每一个小的警告和错误发送电子邮件 如何修改此代码,使其只处理以下事项: 致命语法错误 未定义函数 未能包含文件 看一看 如果要将错误报告设置为 error_reporting(E_ERROR | E_PARSE | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR); 然后希望它能解决上述问题 set\u error\u handler
- 致命语法错误
- 未定义函数
- 未能包含文件
error_reporting(E_ERROR | E_PARSE | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR);
然后希望它能解决上述问题
set\u error\u handler
允许您指定用户定义的错误处理函数,以决定如何处理某些(但仅非致命)错误。然后,您可以以您认为必要的任何方式处理特定类型的错误,例如通过电子邮件通知系统管理员,或保存到特定的日志文件。有关更多详细信息,请参阅
关于您的请求,您可以采用以下方法:
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' );
函数myErrorHandler($errno、$errstr、$errfile、$errline)
{
如果(!(错误报告()&$errno)){
//此错误代码不包括在错误报告中
返回;
}
交换机($errno){
案例E_用户_错误:
案例E_错误:
案例E_编译_错误:
echo“我的错误[$errno]$errstr
\n”;
echo“文件$errfile中的$errline行出现致命错误”;
echo“.PHP”.PHP_VERSION.”(“.PHP_OS.”)
\n”;
回显“正在中止…
\n”;
emailErrorFunction($errno、$erstr、$errfile、$errline);
出口(1);
打破
违约:
echo“未知错误类型:[$errno]$errstr
\n”;
打破
}
/*不执行PHP内部错误处理程序*/
返回true;
}
//报告所有错误
错误报告(E_全部);
//用户可以使用自定义错误处理程序打印输出
设置错误处理程序('myErrorHandler');
由于该错误处理不适用于致命错误(解析错误、未定义的函数等),因此您需要使用相关问题中概述的方法对此进行记录:
register_shutdown_function('mail_on_error'); //inside your php script
/** If any file having any kind of fatal error, sln team will be notified when cron will
become fail : following is the name of handler and its type
E_ERROR: 1 | E_WARNING: 2 | E_PARSE: 4 | E_NOTICE: 8
*/
function mail_on_error() {
global $objSettings;
$error = error_get_last();
//print_r($error);
if ($error['type'] == 1) {
// update file path into db
$objSettings->update_records($id=1,array('fatal_error' => json_encode($error)));
$exception_in_file_path = __FILE__;
fatal_error_structure($exception_in_file_path);
}// end if
}// end mail_on_error
应在某些全局位置上定义致命错误结构。比如function.inc.php
。这将向注册用户发送电子邮件
function fatal_error_structure($exception_in_file_path){
$subject = "FATAL: Cron Daemon has failed";
sln_send_mail(nl2br("Please check $exception_in_file_path, This cron having FATAL error."),
$subject, 'email@address.com',$name_reciever='', 'text/plain');
}// end fatal_error_structure
提醒您可以使用(如果您有shell访问权限)日志观察程序shell脚本。这允许捕获任何错误,包括解析错误(无法使用register\u shutdown\u函数捕获)
#/bin/bash
tail-f/path_to_error_log/php_error.log |在读取行时;做
如果grep-q“PHP(Parse | Fatal)error”hm…似乎没有拾取未定义的函数或PHP解析错误,例如缺少brakets。您可能需要在之前设置错误报告(E_ALL)
。看我更新的答案。不,这似乎也不起作用。我认为致命的解析错误阻止了任何错误报告的发生。“致命错误发生后,无法继续执行脚本-这就是致命的原因!”是的,您可以捕获致命错误。您只有一次机会这样做。这应该被接受,因为它描述了致命错误的处理程序
#!/bin/bash
tail -f /path_to_error_log/php_error.log|while read LINE;do
if grep -q "PHP (Parse|Fatal) error" <(echo $LINE); then
(
echo "From: server@example.com"
echo "To: me@example.com"
echo "Subject: PHP Error"
echo $LINE
) | sendmail -t
fi
done