From 16c179cad71259dab0f68b326cb31da9b3c29d91 Mon Sep 17 00:00:00 2001 From: wjzijderveld Date: Mon, 30 Sep 2024 16:52:38 +0200 Subject: [PATCH 1/2] Only skip real exceptions --- src/Monolog/Handler.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Monolog/Handler.php b/src/Monolog/Handler.php index 3e8d52bba..94061beaf 100644 --- a/src/Monolog/Handler.php +++ b/src/Monolog/Handler.php @@ -59,7 +59,7 @@ protected function doWrite($record): void $hint = new EventHint(); - if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) { + if ($this->hasExceptionContext($record['context'])) { $hint->exception = $record['context']['exception']; } @@ -94,18 +94,12 @@ private function getMonologContextData(array $context): array return []; } - $contextData = []; - - foreach ($context as $key => $value) { - // We skip the `exception` field because it goes in its own context - if ($key === self::CONTEXT_EXCEPTION_KEY) { - continue; - } - - $contextData[$key] = $value; + if ($this->hasExceptionContext($context)) { + // remove the exception from the context, as it's set on the hint + unset($context[self::CONTEXT_EXCEPTION_KEY]); } - return $contextData; + return $context; } /** @@ -127,4 +121,9 @@ private function getMonologExtraData(array $context): array return $extraData; } + + private function hasExceptionContext(array $context): bool + { + return isset($context[self::CONTEXT_EXCEPTION_KEY]) && $context[self::CONTEXT_EXCEPTION_KEY] instanceof \Throwable; + } } From 4456894d0cbfe776052c928abb6b02e8a866933a Mon Sep 17 00:00:00 2001 From: wjzijderveld Date: Mon, 30 Sep 2024 17:11:27 +0200 Subject: [PATCH 2/2] Also cover the change in a test --- src/Monolog/Handler.php | 3 +++ tests/Monolog/HandlerTest.php | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/Monolog/Handler.php b/src/Monolog/Handler.php index 94061beaf..615a62192 100644 --- a/src/Monolog/Handler.php +++ b/src/Monolog/Handler.php @@ -122,6 +122,9 @@ private function getMonologExtraData(array $context): array return $extraData; } + /** + * @param mixed[] $context + */ private function hasExceptionContext(array $context): bool { return isset($context[self::CONTEXT_EXCEPTION_KEY]) && $context[self::CONTEXT_EXCEPTION_KEY] instanceof \Throwable; diff --git a/tests/Monolog/HandlerTest.php b/tests/Monolog/HandlerTest.php index bc1d4dd67..d2668814f 100644 --- a/tests/Monolog/HandlerTest.php +++ b/tests/Monolog/HandlerTest.php @@ -269,6 +269,7 @@ public static function handleDataProvider(): iterable [ 'foo' => 'bar', 'bar' => 'baz', + 'exception' => 'just a message', ], [] ), @@ -280,6 +281,7 @@ public static function handleDataProvider(): iterable 'monolog.context' => [ 'foo' => 'bar', 'bar' => 'baz', + 'exception' => 'just a message', ], ], ];