Skip to content

Commit

Permalink
fix(logging): fix linting and potential double json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
das-nagnag committed Jul 23, 2024
1 parent 90c336a commit 5fb2e35
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Classes/Backend/JSONConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function append(string $message, int $severity = LOG_INFO, $additionalDat
'datetime' => new \DateTime('now')
];
$output = json_encode($data);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$data = [
'severity' => $this->severityLabels[LOG_WARNING],
'service' => $this->serviceContext['service'] ?? '',
Expand Down
14 changes: 13 additions & 1 deletion Classes/ThrowableStorage/ConsoleStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,22 @@ public function logThrowable(\Throwable $throwable, array $additionalData = []):

$serviceContext = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 't3n.FlowLog.serviceContext');

/**
* If the message is already json encoded, decode it, else use plain message
* since we want to ensure that the message is not json encoded twice
*/
json_decode($throwable->getMessage());
if (json_last_error() === JSON_ERROR_NONE) {
$message = json_decode($throwable->getMessage(), true);
} else {
$message = $throwable->getMessage();
}

$data = [
'eventTime' => (new \DateTime('now'))->format(DATE_RFC3339),
'serviceContext' => $serviceContext,
'message' => sprintf('PHP Warning: %s' . PHP_EOL . 'Stack trace:' . PHP_EOL . '%s', $throwable->getMessage(), $throwable->getTraceAsString()),
'message' => $message,
'stackTrace' => $throwable->getTrace(),
'context' => [
'httpRequest' => $this->getHttpRequestContext(),
'reportLocation' => [
Expand Down

0 comments on commit 5fb2e35

Please sign in to comment.