From 81dfd00dc7e536b73f273a26a63cf0e1e5e70dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20M=C3=BChlenpfordt?= Date: Wed, 12 Jun 2024 17:29:52 +0200 Subject: [PATCH] feat(logging): add json console logger backend cherry-picked the JSONConsoleLogger from the main branch in order to release a tag for lower flow versions of this package --- Classes/Backend/JSONConsoleLogger.php | 99 +++++++++++++++++++++++++++ Configuration/Settings.yaml | 16 ++++- 2 files changed, 114 insertions(+), 1 deletion(-) 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..97ef548 --- /dev/null +++ b/Classes/Backend/JSONConsoleLogger.php @@ -0,0 +1,99 @@ +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 ', + ]; + } + + /** + * @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 = 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)) { + fwrite($this->streamHandle, $output . PHP_EOL); + } + } + } +} diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 8e7d342..2873e7c 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -4,4 +4,18 @@ t3n: service: 'flow-app' version: 'master' - +Neos: + Flow: + log: + psr3: + 'Neos\Flow\Log\PsrLoggerFactory': + bigQueryLogger: + default: + class: 't3n\FlowLog\Backend\BigQueryLogger' + options: + loggerName: 'bigQueryLogger' + jsonConsoleLogger: + default: + class: 't3n\FlowLog\Backend\JSONConsoleLogger' + options: + loggerName: 'jsonConsoleLogger'