How can I save a PHP backtrace to the error log?

Leo Jiang picture Leo Jiang · Dec 3, 2011 · Viewed 37.6k times · Source

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?

Answer

Álvaro González picture Álvaro González · Dec 3, 2011

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());