Skip to content

Commit

Permalink
Removed previous exception from logstash (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Aug 16, 2023
1 parent cf71756 commit a0da5cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
38 changes: 9 additions & 29 deletions src/OutputMods/Logstash.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ public function onExecException(\Exception $exception): void
$this->logger->log(
Level::Critical,
'Command Exception: ' . $exception->getMessage(),
$this->prepareContext(['error' => self::exceptionToLog($exception)]),
$this->prepareContext([
'error' => [
'type' => \get_class($exception),
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
'stack_trace' => $exception->getTraceAsString(),
],
]),
);
}

Expand Down Expand Up @@ -132,32 +140,4 @@ protected function prepareContext(array $context): array

return parent::prepareContext($newContext);
}

private static function exceptionToLog(?\Throwable $exception): ?array
{
static $deepCounter = 0;

if ($exception === null) {
return null;
}

$maxExceptionDeepLevel = 5;
$deepCounter++;

if ($deepCounter === $maxExceptionDeepLevel) {
return [
'message' => $exception->getMessage(),
'previous' => 'too deep',
];
}

return [
'type' => \get_class($exception),
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
'stack_trace' => $exception->getTraceAsString(),
'previous' => self::exceptionToLog($exception->getPrevious()),
];
}
}
1 change: 0 additions & 1 deletion tests/CliOutputLogstashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public function testFormatOfMessageException(): void
'message' => 'string',
'file' => 'string',
'stack_trace' => 'string',
'previous' => 'NULL',
],
],
], $lineStruture);
Expand Down

0 comments on commit a0da5cd

Please sign in to comment.