I'm using this right now:
error_log(serialize(debug_backtrace()));
But I have to unserialize it every time. Is there a better way to store backtraces?
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.
Definition and Usage. The debug_backtrace() function generates a PHP backtrace. This function displays data from the code that led up to the debug_backtrace() function. Returns an array of associative arrays.
But, what is a stack trace? In essence, it is a rundown of every file and function that is called leading up to the error. To be clear, a stack trace doesn't include the files and functions that are touched before the error occurred, only the chain of methods that are called as the error happened.
This should generate a readable string:
error_log(print_r(debug_backtrace(), true));
Additionally, debug_print_backtrace() prints the back trace as string and its output can be captured with regular output buffer functions:
ob_start(); debug_print_backtrace(); error_log(ob_get_clean());
From my perspective the best approach is using an exception functionality:
$e = new Exception(); $e->getTraceAsString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With