在PHP中记录解析错误

时间:2017-12-22 06:06:45

标签: php error-handling error-logging custom-error-handling

我正在研究在PHP中记录错误。我创建用户定义错误处理程序并记录错误并将其保存到DB中。我无法记录的错误类型很少。

我的错误处理程序代码...

<?php


/**
  * Parameters:
  *  $errno:   Error level
  *  $errstr:  Error message
  *  $errfile: File in which the error was raised
  *  $errline: Line at which the error occurred
  */

function my_error_handler($errno, $errstr, $errfile, $errline)
{  
  switch ($errno) {
    case E_USER_ERROR:
      // Send an e-mail to the administrator
     // error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL);
 echo "Error: $errstr \n  on line $errline in file $errfile";

 //stack trace
 $host='mysql:host=localhost;dbname=logs';
$user='root';
$password='';
$con=new PDO($host,$user,$password);
$trace=json_encode(debug_backtrace());
$qry=$con->prepare("insert into error_log (error_type,error_message,error_file,error_line,stack_trace) values('".$errno."','".$errstr."','".$errfile."','".$errline."','".$trace."')");
$qry->execute();

      // Write the error to our log file
    //  error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE);
echo "<br>";     
     break;

    case E_USER_WARNING:
      // Write the error to our log file
      //error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
      echo "$errstr \n in $errfile on line $errline \n";
       //stack trace
 $host='mysql:host=localhost;dbname=logs';
$user='root';
$password='';
$con=new PDO($host,$user,$password);
$trace=json_encode(debug_backtrace());
$qry=$con->prepare("insert into error_log (error_type,error_message,error_file,error_line,stack_trace) values('".$errno."','".$errstr."','".$errfile."','".$errline."','".$trace."')");
$qry->execute();

    case E_USER_NOTICE:
      // Write the error to our log file
      //error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);

     echo "$errstr \n in $errfile on line $errline \n";
      //stack trace
 $host='mysql:host=localhost;dbname=logs';
$user='root';
$password='';
$con=new PDO($host,$user,$password);
$trace=json_encode(debug_backtrace());
$qry=$con->prepare("insert into error_log (error_type,error_message,error_file,error_line,stack_trace) values('".$errno."','".$errstr."','".$errfile."','".$errline."','".$trace."')");
$qry->execute();
     echo "<br>";
      break;

    default:
      // Write the error to our log file
      //error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
      break;
  }

  // Don't execute PHP's internal error handler
  return TRUE;
}


// Use set_error_handler() to tell PHP to use our method
$old_error_handler = set_error_handler("my_error_handler");

?>

我想要的错误记录:

trigger_error("Core Error",E_CORE_ERROR);
trigger_error("Core Warning",E_CORE_WARNING);
trigger_error("Compile Error",E_COMPILE_ERROR);
trigger_error("Compile Warning",E_COMPILE_WARNING);
trigger_error("Strict Error",E_STRICT);
trigger_error("Recoverable Error", E_RECOVERABLE_ERROR);
trigger_error("Depreciated Error",E_DEPRECATED);
trigger_error("parse error",E_PARSE);
trigger_error("Error",E_ERROR);

我想记录上面提到的所有错误,比如像分号等代码中缺少某些东西然后我们得到了语法错误等...所以我想记录所有错误.... 我搜索了很多,但找不到最符合我需求的解决方案,并且在大多数地方它说使用自定义错误处理程序无法记录这些错误,但我确信这些错误可以记录,因为它也是我们在 php_error_log 文件中检查时记录,所以我希望WAY使用自定义错误处理程序记录它... 任何帮助将不胜感激。

  

由于代码i,请不要评论或拒绝投票   粘贴在这里,这个代码正在开发中,我没有   干净,保护它,只发布根据/与问题相关。

0 个答案:

没有答案