Skip to content

Commit

Permalink
Using whoops to generate the stack for Log::ex
Browse files Browse the repository at this point in the history
  • Loading branch information
enobrev committed Aug 4, 2022
1 parent 45c2708 commit b1f9aec
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ext-json": "*",
"adbario/php-dot-notation": "^3.1",
"doctrine/inflector": "^2.0",
"filp/whoops": "^2.14",
"laminas/laminas-diactoros": "^2.2",
"laminas/laminas-httphandlerrunner": "^2.0",
"monolog/monolog": "^2.0"
Expand Down
73 changes: 72 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 7 additions & 20 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,33 +379,20 @@ public static function e(string $sMessage, array $aContext = array()): bool {
* @return boolean Whether the record has been processed
*/
public static function ex(string $sMessage, Throwable $oThrowable, array $aContext = array()): bool {
$iTruncate = self::$iStackLimit;
$aStack = $oThrowable->getTrace();
$iStack = count($aStack);

if ($iStack > $iTruncate) {
$iRemaining = $iStack - $iTruncate;
$aStack = array_slice($aStack, 0, 5);
$aStack[] = [
"__TRUNCATED__" => "$iRemaining entries cut from $iStack stack entries for brevity"
];
}

$aStackCopy = [];
foreach($aStack as $aItem) { // Do NOT use a reference here as getTrace returns references to the actual args in the call stack which can then modify the real vars in the stack
if (isset($aItem['args'])) {
$aItem['args'] = self::replaceObjects($aItem['args']);
}
$aStackCopy[] = $aItem;
}
$oWhoops = new \Whoops\Run;
$oWhoops->allowQuit(false);
$oWhoops->writeToOutput(false);
$oWhoopsHandler = new \Whoops\Handler\JsonResponseHandler();
$oWhoopsHandler->addTraceToOutput(true);
$oWhoops->pushHandler($oWhoopsHandler);

$aContext['--exception'] = [
'type' => get_class($oThrowable),
'code' => $oThrowable->getCode(),
'message' => $oThrowable->getMessage(),
'file' => $oThrowable->getFile(),
'line' => $oThrowable->getLine(),
'stack' => json_encode($aStackCopy)
'stack' => $oWhoops->handleException($oThrowable)
];

$aContext['--span'] = [
Expand Down

0 comments on commit b1f9aec

Please sign in to comment.