php - 在可调用的set_error_handler中获取产生错误的函数名称

标签 php error-handling

我在php中设置自定义错误处理程序,如下所示:

<?php  

function custom_error_handler($severity, $message, $file, $line, $errcontext) 
{
    //Need to get function name
    echo sprintf("\n%s [%s@L%s] : %s \n", $severity, $file, $line, $message);
}

function test_error() 
{
    trigger_error("Just a user generated error");
}

set_error_handler( function($severity, $message, $file, $line, $errcontext) {
    custom_error_handler($severity, $message, $file, $line, $errcontext);
} );

test_error();

custom_error_handler范围内是否有某种方法可以获取触发/生成错误的函数名称,在这种情况下为test_error()
我已经阅读了debug_backtrace()文档,但不确定是否要这样做。

谢谢。

最佳答案

是的,在那种情况下,debug_backtrace是您所需要的,但是您还需要知道您真正想要跟踪的内容。

查看示例代码:

<?php  

function custom_error_handler($severity, $message, $file, $line, $errcontext) 
{
      $dt = debug_backtrace();
      $function = '';      

      $remove = array(__FUNCTION__,'trigger_error', '{closure}');

       foreach ($dt as $dti) {
             if (isset($dti['function']) && !in_array($dti['function'],$remove)) {
                 $function = $dti['function'];
                 break;
             }

       }


    //Need to get function name
    echo sprintf("\n%s [%s@L%s@%s] : %s \n", $severity, $file, $line, $function, $message)."<br />";


}

function test_error() 
{
    trigger_error("Just a user generated error");
}

set_error_handler( function($severity, $message, $file, $line, $errcontext) {
    custom_error_handler($severity, $message, $file, $line, $errcontext);
} );

function bla_error() {
    test_error();    
}

bla_error();
trigger_error("Just a user generated error");
test_error();

在这种情况下,您将返回:
1024 [D:\DaneAplikacji\easyphp\data\localweb\error.php@L27@test_error] : Just a user generated error
1024 [D:\DaneAplikacji\easyphp\data\localweb\error.php@L39@] : Just a user generated error
1024 [D:\DaneAplikacji\easyphp\data\localweb\error.php@L27@test_error] : Just a user generated error 

因此,在bla_error和test_error的两种情况下,您都会获得函数test_error,因为在该函数中最终会启动trigger_error,但是当您不使用 break 时,您将得到最终函数,它会启动test_error(如果有)

我个人想在数据库类中使用backtrace_ignore,以保存查询的文件名和使用代码启动查询的行:
private function fetchLauncher() 
{
  $key = 0;     
  $dt = debug_backtrace();

  for ($i=0,$c=count($dt);$i<$c;++$i) {
   if (strpos($dt[$i]['file'],$this->settings['backtrace_ignore'])===false) {
     $key = $i;
     break;  
   }      
  }


   $this->launcher['file'] = $dt[$key]['file'];
   $this->launcher['line'] = $dt[$key]['line'];              
}

(作为$ this-> settings ['backtrace_ignore'],我使用数据库类文件名来查找正确的文件)。

关于php - 在可调用的set_error_handler中获取产生错误的函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22331743/

相关文章:

node.js - 错误处理中间件并不总是有效

jsf-2 - JSF 2.0 中的 WEB.XML 错误页面

php - #1193 - 尝试登录 phpmyadmin 时出现未知系统变量 'lc_messages'

php - 通过虚拟主机配置设置 Application_ENV 并在 PHP 中读取

PHP parse_ini_file 错误

Oracle:ORA-XXXXX 代码的符号名称和逻辑分组?

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

javascript - Mousedown 仍在提交表单,而它不应该

php - 如何拆分一个字符串并找到一个字符串在另一个字符串中的出现?

r - 删除功能使其停止在R中的元素