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

二次信任 提交于 2019-12-02 17:39:49

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());
Pramendra Gupta
    $log = var_export(debug_backtrace(), true);

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

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

$e = new Exception();
$e->getTraceAsString();
Kzqai

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

The following can either be written to a .txt file or you can also access it's contents (like $content[0]) as opposed to var_export which is a bit trickier I find:

    $content = unserialize(serialize(debug_backtrace()));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!