php - debug_print_backtrace() to String for log-file

ID : 274399

viewed : 9

Tags : phpdebuggingloggingphp





Top 4 Answer for php - debug_print_backtrace() to String for log-file

vote vote

97

Use another function. debug_backtrace() returns an array that you can loop through, format and save:

$data = debug_backtrace(); 

Or use output buffering for the formatted output string:

ob_start(); debug_print_backtrace(); $data = ob_get_clean(); 
vote vote

89

It's possible to do it with even less code, actually. Avoid the overhead of buffering with...

$error_string = (new Exception)->getTraceAsString(); 

That gives you the exact same output as debug_print_backtrace() stored to the $error_string variable.

And if you want to get more information for a more valuable stacktrace (line numbers, local object vars, etc.), try...

$error_string = print_r($e->getTrace(), true); 
vote vote

78

To add on to the answer given by @HoldOffHunger, the following would have been sufficient for logging purpose:

$this->log(print_r(debug_backtrace(), true)); 
vote vote

62

Top 3 video Explaining php - debug_print_backtrace() to String for log-file







Related QUESTION?