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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
set_error_handler()
+debug_backtrace()
+debug_print_backtrace()
in PHP5I just tried setting a session variable containing the contents of debug_backtrace() at the offending line, then printing it using register_shutdown_function(). Worked like a charm.
If you can't install a debugger then use this function sorrounding the fatal error to get the "fatal stack". Check the code and example below that explains better how to use it:
Here's an example of how to use it:
Finally to read the contents of the log...
Hope that helps!
You can use debug_backtrace
PHP Error
This is better error reporting for PHP written in PHP. No extra extensions are required!
It is trivial to use where all errors are displayed in the browser for normal, AJAXy requests (in paused state). Then all errors provides you with a backtrace and code context across the whole stack trace, including function arguments, server variables.
All you need to do is to include one single file and call the function (at the beginning on your code), e.g.
See the screenshots:
GitHub: https://github.com/JosephLenton/PHP-Error
My fork (with extra fixes): https://github.com/kenorb-contrib/PHP-Error
Debug PHP class
A complete PHP debugger class, with support for Exception, Errors, Alerts ( from user), code lines and highlight flags.
Example usage:
Error Handling in PHP
The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function:
Shorter way (PHP):
Longer way (PHP):
See: http://www.php.net/manual/en/function.set-error-handler.php
Note: You can only have one error exception at a time. When you call the set_error_handler() function it will return the name of the old error handler. You can store this and call it yourself from your error handler – thus allowing you to have multiple error handlers.
XDebug
For more advanced solution, you can use XDebug extension for PHP.
By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug.auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.profiler_enable) or other settings. If the trace file is too big, you can use xdebug_start_trace() and xdebug_stop_trace() to dump the partial trace.
Installation
Using PECL:
On Linux:
On Mac (with Homebrew):
Example of mine configuration:
Drupal 6&7
With Devel enabled:
Above function will log the backtraces on each error into temporary file (
/tmp/drupal_debug.txt
by default).Or locate the file via:
drush eval "echo file_directory_temp() . '/drupal_debug.txt'
.Without Devel enabled, use old school approach:
var_dump(debug_backtrace());
instead ofdd()
.