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

前端 未结 6 2096
无人共我
无人共我 2021-01-31 02:34

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 bac

相关标签:
6条回答
  • 2021-01-31 02:57
        $log = var_export(debug_backtrace(), true);
    

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

    0 讨论(0)
  • 2021-01-31 03:00

    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()));
    
    0 讨论(0)
  • 2021-01-31 03:01

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

    $e = new Exception();
    $e->getTraceAsString();
    
    0 讨论(0)
  • 2021-01-31 03:03

    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());
    
    0 讨论(0)
  • 2021-01-31 03:06

    For those who might want a more compact version, this will also do the trick:

    error_log((new Exception())->getTraceAsString())
    
    0 讨论(0)
  • 2021-01-31 03:21

    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()));
    
    0 讨论(0)
提交回复
热议问题