From 074503850ba6ebbc1b32e7bb7491d45ed257c3c7 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 5 Dec 2024 18:02:26 +0100 Subject: [PATCH] Remove empty lines if a custom stack trace parser returns null, fixes #1925 --- src/Monolog/Formatter/LineFormatter.php | 2 +- tests/Monolog/Formatter/LineFormatterTest.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index e563503f0..8c671aa99 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -309,6 +309,6 @@ private function stacktracesParser(\Throwable $e): string private function stacktracesParserCustom(string $trace): string { - return implode("\n", array_filter(array_map($this->stacktracesParser, explode("\n", $trace)), fn ($line) => $line !== false && $line !== '')); + return implode("\n", array_filter(array_map($this->stacktracesParser, explode("\n", $trace)), fn ($line) => is_string($line) && trim($line) !== '')); } } diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php index 8408a77b5..6f08f76d0 100644 --- a/tests/Monolog/Formatter/LineFormatterTest.php +++ b/tests/Monolog/Formatter/LineFormatterTest.php @@ -193,10 +193,9 @@ public function testDefFormatWithExceptionAndStacktraceParserEmpty() }); $message = $formatter->format($this->getRecord(Level::Critical, context: ['exception' => new \RuntimeException('Foo')])); - $trace = explode('[stacktrace]', $message, 2)[1]; - $this->assertStringNotContainsString('#', $trace); + $this->assertSame(PHP_EOL . PHP_EOL . '"} []' . PHP_EOL, $trace); } public function testDefFormatWithPreviousException()