From 5fb2e35d83cb02318ed40f4f3900f9d6e894c8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20M=C3=BChlenpfordt?= Date: Tue, 23 Jul 2024 18:09:03 +0200 Subject: [PATCH] fix(logging): fix linting and potential double json encoding --- Classes/Backend/JSONConsoleLogger.php | 2 +- Classes/ThrowableStorage/ConsoleStorage.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Classes/Backend/JSONConsoleLogger.php b/Classes/Backend/JSONConsoleLogger.php index 05c28b1..2f233d5 100644 --- a/Classes/Backend/JSONConsoleLogger.php +++ b/Classes/Backend/JSONConsoleLogger.php @@ -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'] ?? '', diff --git a/Classes/ThrowableStorage/ConsoleStorage.php b/Classes/ThrowableStorage/ConsoleStorage.php index 1ad2f46..d3cbbe6 100644 --- a/Classes/ThrowableStorage/ConsoleStorage.php +++ b/Classes/ThrowableStorage/ConsoleStorage.php @@ -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' => [