From b61b3cb18294302c3108ef28420edd3c1c429779 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 28 Mar 2021 21:50:32 +0200 Subject: [PATCH] Fix object handling inside log arrays --- src/Util/Logger/AbstractLogger.php | 22 ++++++++++++++++++++ src/Util/Logger/StreamLogger.php | 4 ++-- src/Util/Logger/SyslogLogger.php | 4 ++-- tests/src/Util/Logger/AbstractLoggerTest.php | 19 +++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Util/Logger/AbstractLogger.php b/src/Util/Logger/AbstractLogger.php index 4d653ae9b6..110a806874 100644 --- a/src/Util/Logger/AbstractLogger.php +++ b/src/Util/Logger/AbstractLogger.php @@ -103,6 +103,28 @@ abstract class AbstractLogger implements LoggerInterface return strtr($message, $replace); } + /** + * JSON Encodes an complete array including objects with "__toString()" methods + * + * @param array $input an Input Array to encode + * + * @return false|string The json encoded output of the array + */ + protected function jsonEncodeArray(array $input) + { + $output = []; + + foreach ($input as $key => $value) { + if (method_exists($value, '__toString')) { + $output[$key] = $value->__toString(); + } else { + $output[$key] = $value; + } + } + + return @json_encode($output); + } + /** * {@inheritdoc} */ diff --git a/src/Util/Logger/StreamLogger.php b/src/Util/Logger/StreamLogger.php index 035e7ecee4..5912e70904 100644 --- a/src/Util/Logger/StreamLogger.php +++ b/src/Util/Logger/StreamLogger.php @@ -161,8 +161,8 @@ class StreamLogger extends AbstractLogger $logMessage .= $this->channel . ' '; $logMessage .= '[' . strtoupper($level) . ']: '; $logMessage .= $this->psrInterpolate($message, $context) . ' '; - $logMessage .= @json_encode($context) . ' - '; - $logMessage .= @json_encode($record); + $logMessage .= $this->jsonEncodeArray($context) . ' - '; + $logMessage .= $this->jsonEncodeArray($record); $logMessage .= PHP_EOL; return $logMessage; diff --git a/src/Util/Logger/SyslogLogger.php b/src/Util/Logger/SyslogLogger.php index 101baad38a..ae39ad9588 100644 --- a/src/Util/Logger/SyslogLogger.php +++ b/src/Util/Logger/SyslogLogger.php @@ -195,8 +195,8 @@ class SyslogLogger extends AbstractLogger $logMessage .= $this->channel . ' '; $logMessage .= '[' . $this->logToString[$level] . ']: '; $logMessage .= $this->psrInterpolate($message, $context) . ' '; - $logMessage .= @json_encode($context) . ' - '; - $logMessage .= @json_encode($record); + $logMessage .= $this->jsonEncodeArray($context) . ' - '; + $logMessage .= $this->jsonEncodeArray($record); return $logMessage; } diff --git a/tests/src/Util/Logger/AbstractLoggerTest.php b/tests/src/Util/Logger/AbstractLoggerTest.php index 33bf235616..e9f183081d 100644 --- a/tests/src/Util/Logger/AbstractLoggerTest.php +++ b/tests/src/Util/Logger/AbstractLoggerTest.php @@ -159,4 +159,23 @@ abstract class AbstractLoggerTest extends MockedTest self::assertContains(@json_encode($context), $text); } + + /** + * Test a message with an exception + */ + public function testExceptionHandling() + { + $e = new \Exception("Test String", 123); + $eFollowUp = new \Exception("FollowUp", 456, $e); + + $assertion = $eFollowUp->__toString(); + + $logger = $this->getInstance(); + $logger->alert('test', ['e' => $eFollowUp]); + $text = $this->getContent(); + + self::assertLogline($text); + + self::assertContains(@json_encode($assertion), $this->getContent()); + } }