如何通过电子邮件发送PHP致命错误?

Dav*_*vid 15 php syntax error-handling

我正在完善一大堆代码.很多它包含许多不影响代码执行的一般警告和注意事项(即:未定义的变量,或没有qoutes的数组键).

我想编写一个函数,让我首先专注于致命错误,然后我会打开不那么紧急的警告和通知.我找到了这个代码,但它通过电子邮件发送每个小警告通知和错误.

http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/

如何修改此代码,使其仅处理以下内容:

  • 致命的语法错误
  • 未定义的功能
  • 无法包含文件

Gor*_*dyD 12

set_error_handler允许您指定用户定义的错误处理函数,以决定如何处理某些(但仅非致命)错误.然后,您可以以您认为必要的任何方式处理特定类型的错误,例如通过电子邮件通知系统管理员,或保存到特定的日志文件.请参阅: PHP Doc以获取更多详细信息.

关于您的请求,您可以采用以下方法:

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' );
Run Code Online (Sandbox Code Playgroud)

由于错误处理不适用于致命错误(解析错误,未定义的函数等),因此您需要register_shutdown_function按照相关问题中的说明对其进行磁带处理:


Vin*_*shi 6

在 php 文件中初始化以下函数。

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
Run Code Online (Sandbox Code Playgroud)

fatal_error_structure应该在某个全球位置定义。喜欢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
Run Code Online (Sandbox Code Playgroud)


dem*_*tal 5

或者,您可以使用(如果您有 shell 访问权限)日志观察程序 shell 脚本。这允许捕获任何错误,包括解析错误(您无法使用 register_shutdown_function 捕获)。

#!/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
Run Code Online (Sandbox Code Playgroud)

恕我直言,这更无害,因为这只是一个侧面过程,并且允许您依赖其他技术(shell 脚本或其他技术),万一由于 php 环境本身而发生错误,您仍然会收到通知。