Home Backend Development PHP Tutorial PHP 打印调用堆栈信息

PHP 打印调用堆栈信息

Jun 20, 2016 pm 12:32 PM

在PHP中发生错我,我们使用set_error_handler进行处理,如果发生异常,则使用set_exception_handler,但是在调试中,我们也可以使用

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

set_error_handler的使用

<?php// error handler functionfunction 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:        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";        exit(1);        break;    case E_USER_WARNING:        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";        break;    case E_USER_NOTICE:        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";        break;    default:        echo "Unknown error type: [$errno] $errstr<br />\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");?>
Copy after login

set_exception_handler的使用

<?phpfunction exceptionHandler($exception) {    // these are our templates    $traceline = "#%s %s(%s): %s(%s)";    $msg = "PHP Fatal error:  Uncaught exception '%s' with message '%s' in %s:%s\nStack trace:\n%s\n  thrown in %s on line %s";    // 打印异常调用堆栈信息    $trace = $exception->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);}?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles