• 技术文章 >后端开发 >php教程

    PHP 打印调用堆栈信息

    2016-06-20 12:32:51原创1174
    在PHP中发生错我,我们使用set_error_handler进行处理,如果发生异常,则使用set_exception_handler,但是在调试中,我们也可以使用

    debug_print_backtrace和debug_backtrace进行调用堆栈信息打印

    set_error_handler的使用

    My ERROR [$errno] $errstr
    \n"; echo " Fatal error on line $errline in file $errfile"; echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")
    \n"; echo "Aborting...
    \n"; exit(1); break; case E_USER_WARNING: echo "My WARNING [$errno] $errstr
    \n"; break; case E_USER_NOTICE: echo "My NOTICE [$errno] $errstr
    \n"; break; default: echo "Unknown error type: [$errno] $errstr
    \n"; break; } /* Don't execute PHP internal error handler */ return true;}// function to test the error handlingfunction scale_by_log($vect, $scale){ if (!is_numeric($scale) || $scale <= 0) { trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR); } if (!is_array($vect)) { trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING); return null; } $temp = array(); foreach($vect as $pos => $value) { if (!is_numeric($value)) { trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE); $value = 0; } $temp[$pos] = log($scale) * $value; } return $temp;}$old_error_handler = set_error_handler("myErrorHandler");?>

    set_exception_handler的使用

    getTrace();    foreach ($trace as $key => $stackPoint) {       //返回异常类似,异常描述信息        $trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);    }    // 格式化异常信息    $result = array();    foreach ($trace as $key => $stackPoint) {        $result[] = sprintf(            $traceline,            $key,            $stackPoint['file'],            $stackPoint['line'],            $stackPoint['function'],            implode(', ', $stackPoint['args'])        );    }    // trace always ends with {main}    $result[] = '#' . ++$key . ' {main}';    // write tracelines into main template    $msg = sprintf(        $msg,        get_class($exception),        $exception->getMessage(),        $exception->getFile(),        $exception->getLine(),        implode("\n", $result),        $exception->getFile(),        $exception->getLine()    );    error_log($msg);}?>
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:PHP 5.6.20 发布 下一篇:ubuntu atp安装php后无法运行 提示502
    PHP编程就业班

    相关文章推荐

    • PHP高并发实例详解之解决商品库存超卖问题• 带你聊聊PHP中的泛型之基础知识浅析• 十天学会php之第三天_php基础• 基于php实现七牛抓取远程图片_php实例• php定时执行任务设置详解_php实例

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网