From 8f473a675c86b80cb16d46542601094567f546c7 Mon Sep 17 00:00:00 2001 From: Alexander Mahrt Date: Wed, 25 Jan 2023 16:02:11 +0100 Subject: [PATCH 1/3] feature/MAIN-154: Add new logger backend (json console stdout) --- Classes/Backend/JSONConsoleLogger.php | 90 +++++++++++++++++++++++++++ Configuration/Settings.yaml | 3 + 2 files changed, 93 insertions(+) create mode 100644 Classes/Backend/JSONConsoleLogger.php diff --git a/Classes/Backend/JSONConsoleLogger.php b/Classes/Backend/JSONConsoleLogger.php new file mode 100644 index 0000000..f245359 --- /dev/null +++ b/Classes/Backend/JSONConsoleLogger.php @@ -0,0 +1,90 @@ +severityLabels = [ + LOG_EMERG => 'emergency', + LOG_ALERT => 'alert', + LOG_CRIT => 'critical', + LOG_ERR => 'error', + LOG_WARNING => 'warning ', + LOG_NOTICE => 'notice', + LOG_INFO => 'info', + LOG_DEBUG => 'debug', + ]; + } + + public function append(string $message, int $severity = LOG_INFO, $additionalData = null, string $packageKey = null, string $className = null, string $methodName = null): void + { + if ($severity > $this->severityThreshold) { + return; + } + + $additionalData = $additionalData ?? []; + + if ($packageKey !== null) { + $additionalData['packageKey'] = $packageKey; + } + + if ($className !== null) { + $additionalData['className'] = $className; + } + + if ($methodName !== null) { + $additionalData['methodName'] = $methodName; + } + + $severityLabel = $this->severityLabels[$severity] ?? 'unknown'; + try { + $data = [ + 'severity' => $severityLabel, + 'service' => $this->serviceContext['service'], + 'version' => $this->serviceContext['version'], + 'message' => $message, + 'additionalData' => json_encode($additionalData, JSON_THROW_ON_ERROR), + 'date' => new Date(new \DateTime('now')), + 'datetime' => new \DateTime('now') + ]; + $output = json_encode($data); + } catch (Exception $e) { + $data = [ + 'severity' => $this->severityLabels[LOG_WARNING], + 'service' => $this->serviceContext['service'], + 'version' => $this->serviceContext['version'], + 'message' => 'Could not decode additional data of log message.', + 'additionalData' => [ + 'previousLog' => [ + 'message' => $message, + 'severity' => $severityLabel, + ], + 'stackTrace' => $e->getTraceAsString() + ], + 'date' => new Date(new \DateTime('now')), + 'datetime' => new \DateTime('now') + ]; + $output = json_encode($data); + } finally { + if (is_resource($this->streamHandle)) { + fputs($this->streamHandle, $output . PHP_EOL); + } + } + } +} diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 9c905fa..4866e13 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -52,3 +52,6 @@ Neos: class: 't3n\FlowLog\Backend\BigQueryLogger' options: loggerName: 'bigQueryLogger' + jsonConsoleLogger: + default: + class: 't3n\FlowLog\Backend\JSONConsoleLogger' From f80d63dc89696967d4be318d3f557cbb153116f4 Mon Sep 17 00:00:00 2001 From: Alexander Mahrt Date: Wed, 25 Jan 2023 16:15:18 +0100 Subject: [PATCH 2/3] feature/MAIN-154: Fix linting --- Classes/Backend/JSONConsoleLogger.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Classes/Backend/JSONConsoleLogger.php b/Classes/Backend/JSONConsoleLogger.php index f245359..a777b27 100644 --- a/Classes/Backend/JSONConsoleLogger.php +++ b/Classes/Backend/JSONConsoleLogger.php @@ -1,5 +1,6 @@ $this->severityThreshold) { return; @@ -83,7 +86,7 @@ public function append(string $message, int $severity = LOG_INFO, $additionalDat $output = json_encode($data); } finally { if (is_resource($this->streamHandle)) { - fputs($this->streamHandle, $output . PHP_EOL); + fwrite($this->streamHandle, $output . PHP_EOL); } } } From 09fe91aa76449ed0fc102ab5c5c14256a57abbfd Mon Sep 17 00:00:00 2001 From: Alexander Mahrt Date: Wed, 25 Jan 2023 16:17:24 +0100 Subject: [PATCH 3/3] feature/MAIN-154: Fix linting... again. --- Classes/Backend/JSONConsoleLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Backend/JSONConsoleLogger.php b/Classes/Backend/JSONConsoleLogger.php index a777b27..9861f5b 100644 --- a/Classes/Backend/JSONConsoleLogger.php +++ b/Classes/Backend/JSONConsoleLogger.php @@ -35,7 +35,7 @@ public function open(): void /** * @param mixed $additionalData A variable containing more information about the event to be logged */ - public function append(string $message, int $severity = LOG_INFO, $additionalData = null, string $packageKey, string $className, string $methodName): void + public function append(string $message, string $packageKey, string $className, string $methodName, int $severity = LOG_INFO, $additionalData = null): void { if ($severity > $this->severityThreshold) { return;