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

2019-03-09 13:12发布

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?

4条回答
祖国的老花朵
2楼-- · 2019-03-09 13:45
    $log = var_export(debug_backtrace(), true);

Then use the variable $log to log in file or what ever.

查看更多
淡お忘
3楼-- · 2019-03-09 13:48

From my perspective the best approach is using an exception functionality:

$e = new Exception();
$e->getTraceAsString();
查看更多
Luminary・发光体
4楼-- · 2019-03-09 13:53

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());
查看更多
别忘想泡老子
5楼-- · 2019-03-09 14:06

A little ugly but workable, I do this:

 error_log('Identifying string so that it doesn\'t just end up as gibberish' . json_encode(debug_backtrace()));
查看更多
登录 后发表回答