一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php Exception打印error trace 实例

时间:2013-09-21 编辑:简简单单 来源:一聚教程网


对于php的Exception,可以通过getTraceAsString获得错误栈。


而对于error则应该如下:

 代码如下 复制代码

function x()
 $x = debug_backtrace();
 //remove stack of this function
 array_shift($x);
 $i = 0;
 $xstr = '';
 foreach ($trace as $x) {
  //TODO need check exists args
  $args = $x['args'];
  if (!$args) {
   $argstr = '';
  } else {
   $argstr = '';
   $first = true;
   foreach ($args as $arg) {
    if (!$first) {
     $argstr .= ',';
    }
    $first = false;
    if (is_object($arg)) {
     $argstr .= 'Object[' . get_class($arg) . ']';
    }
    elseif (is_array($arg)) {
     $argstr .= 'Array';
    } else {
     $argstr .= $arg;
    }
   }
  }
  $xstr .= "[#$i] {$x['file']}({$x['line']}) {$x['function']} ($argstr)" . PHP_EOL;
  $i++;
 }

 return $xstr;
}

热门栏目