How can I get PHP to produce a backtrace upon errors?

后端 未结 11 2125
無奈伤痛
無奈伤痛 2020-11-28 23:29

Trying to debug PHP using its default current-line-only error messages is horrible. How can I get PHP to produce a backtrace (stack trace) when errors are produced?

11条回答
  •  生来不讨喜
    2020-11-29 00:11

    My script for installing an error handler that produces a backtrace:

    ') . ' ' . (isset($item['line']) ? $item['line'] : '') . ' calling ' . $item['function'] . '()' . "\n";
        } else {
            echo '

    ' . "\n"; echo ' Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n"; echo '

      ' . "\n"; foreach($trace as $item) echo '
    1. ' . (isset($item['file']) ? $item['file'] : '') . ' ' . (isset($item['line']) ? $item['line'] : '') . ' calling ' . $item['function'] . '()
    2. ' . "\n"; echo '
    ' . "\n"; echo '

    ' . "\n"; } if(ini_get('log_errors')) { $items = array(); foreach($trace as $item) $items[] = (isset($item['file']) ? $item['file'] : '') . ' ' . (isset($item['line']) ? $item['line'] : '') . ' calling ' . $item['function'] . '()'; $message = 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ': ' . join(' | ', $items); error_log($message); } if($fatal) exit(1); } set_error_handler('process_error_backtrace'); ?>

    Caveat: it is powerless to affect various 'PHP Fatal Errors', since Zend in their wisdom decided that these would ignore set_error_handler(). So you still get useless final-location-only errors with those.

提交回复
热议问题