Skip to content

Instantly share code, notes, and snippets.

@pgorod
Created February 15, 2018 20:31
Show Gist options
  • Save pgorod/a6975280ff795cc42bf72ab2eef05db5 to your computer and use it in GitHub Desktop.
Save pgorod/a6975280ff795cc42bf72ab2eef05db5 to your computer and use it in GitHub Desktop.
Dumps backtrace into log, with chronological order
Call with
$GLOBALS['log']->debug(generateCallTrace());
----------------
function generateCallTrace()
{
$e = new Exception();
$trace = explode("\n", $e->getTraceAsString());
// reverse array to make steps line up chronologically
$trace = array_reverse($trace);
array_shift($trace); // remove {main}
array_pop($trace); // remove call to this method
$length = count($trace);
$result = array();
for ($i = 0; $i < $length; $i++)
{
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', se$
}
return "\t" . implode("\n\t", $result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment