From 467e764fb6ebf5c931ba3edffda938ff755cad19 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 16 Mar 2025 14:03:31 +0100 Subject: [PATCH] Fix gelf message formatter failing to handle context/extra numeric keys, fixes #1932 --- src/Monolog/Formatter/GelfMessageFormatter.php | 4 ++-- tests/Monolog/Formatter/GelfMessageFormatterTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Formatter/GelfMessageFormatter.php b/src/Monolog/Formatter/GelfMessageFormatter.php index d169e1b6..12a3b0a3 100644 --- a/src/Monolog/Formatter/GelfMessageFormatter.php +++ b/src/Monolog/Formatter/GelfMessageFormatter.php @@ -115,7 +115,7 @@ class GelfMessageFormatter extends NormalizerFormatter } foreach ($extra as $key => $val) { - $key = (string) preg_replace('#[^\w\.\-]#', '-', $key); + $key = (string) preg_replace('#[^\w.-]#', '-', (string) $key); $val = \is_scalar($val) || null === $val ? $val : $this->toJson($val); $len = \strlen($this->extraPrefix . $key . $val); if ($len > $this->maxLength) { @@ -127,7 +127,7 @@ class GelfMessageFormatter extends NormalizerFormatter } foreach ($context as $key => $val) { - $key = (string) preg_replace('#[^\w\.\-]#', '-', $key); + $key = (string) preg_replace('#[^\w.-]#', '-', (string) $key); $val = \is_scalar($val) || null === $val ? $val : $this->toJson($val); $len = \strlen($this->contextPrefix . $key . $val); if ($len > $this->maxLength) { diff --git a/tests/Monolog/Formatter/GelfMessageFormatterTest.php b/tests/Monolog/Formatter/GelfMessageFormatterTest.php index bd10d374..a882e76c 100644 --- a/tests/Monolog/Formatter/GelfMessageFormatterTest.php +++ b/tests/Monolog/Formatter/GelfMessageFormatterTest.php @@ -67,7 +67,7 @@ class GelfMessageFormatterTest extends MonologTestCase channel: 'meh', context: ['from' => 'logger'], datetime: new \DateTimeImmutable("@0"), - extra: ['file' => 'test', 'line' => 14], + extra: ['file' => 'test', 'line' => 14, 0 => 'foo'], ); $message = $formatter->format($record);