From e645c11816a580b00dfe8425250eaeb9c0987e7a Mon Sep 17 00:00:00 2001 From: Richard Steinmetz Date: Sat, 28 Feb 2026 17:51:28 +0100 Subject: [PATCH] chore(refactor): improve logger adapter code Signed-off-by: Richard Steinmetz --- .../LoggerInterfaceToOutputAdapter.php | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/Support/LoggerInterfaceToOutputAdapter.php b/lib/Support/LoggerInterfaceToOutputAdapter.php index 8c40833..554158b 100644 --- a/lib/Support/LoggerInterfaceToOutputAdapter.php +++ b/lib/Support/LoggerInterfaceToOutputAdapter.php @@ -9,6 +9,7 @@ namespace OCA\PreviewGenerator\Support; +use JsonException; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Stringable; @@ -21,19 +22,19 @@ public function __construct( } public function emergency(string|Stringable $message, array $context = []): void { - $this->writeToOutput('error', $message, $context); + $this->writeToOutput('error', (string)$message, $context); } public function alert(string|Stringable $message, array $context = []): void { - $this->writeToOutput('error', $message, $context); + $this->writeToOutput('error', (string)$message, $context); } public function critical(string|Stringable $message, array $context = []): void { - $this->writeToOutput('error', $message, $context); + $this->writeToOutput('error', (string)$message, $context); } public function error(string|Stringable $message, array $context = []): void { - $this->writeToOutput('error', $message, $context); + $this->writeToOutput('error', (string)$message, $context); } public function warning(string|Stringable $message, array $context = []): void { @@ -41,7 +42,7 @@ public function warning(string|Stringable $message, array $context = []): void { return; } - $this->writeToOutput(null, $message, $context); + $this->writeToOutput(null, (string)$message, $context); } public function notice(string|Stringable $message, array $context = []): void { @@ -49,7 +50,7 @@ public function notice(string|Stringable $message, array $context = []): void { return; } - $this->writeToOutput(null, $message, $context); + $this->writeToOutput(null, (string)$message, $context); } public function info(string|Stringable $message, array $context = []): void { @@ -57,7 +58,7 @@ public function info(string|Stringable $message, array $context = []): void { return; } - $this->writeToOutput(null, $message, $context); + $this->writeToOutput(null, (string)$message, $context); } public function debug(string|Stringable $message, array $context = []): void { @@ -65,7 +66,7 @@ public function debug(string|Stringable $message, array $context = []): void { return; } - $this->writeToOutput(null, $message, $context); + $this->writeToOutput(null, (string)$message, $context); } public function log($level, $message, array $context = []): void { @@ -81,20 +82,18 @@ public function log($level, $message, array $context = []): void { }; } - private function writeToOutput( - ?string $decorator, - string|Stringable $message, - array $context = [], - ): void { - $message = (string)$message; + private function writeToOutput(?string $decorator, string $message, array $context = []): void { if (!empty($context)) { - $message .= ' ' . json_encode($context); + try { + $message .= ' ' . json_encode($context, JSON_THROW_ON_ERROR); + } catch (JsonException $e) { + } } if ($decorator) { - $this->output->writeln("<$decorator>$message"); - } else { - $this->output->writeln($message); + $message = "<$decorator>$message"; } + + $this->output->writeln($message); } }