Răsfoiți Sursa

Fix gelf message formatter failing to handle context/extra numeric keys, fixes #1932

Jordi Boggiano 10 luni în urmă
părinte
comite
467e764fb6

+ 2 - 2
src/Monolog/Formatter/GelfMessageFormatter.php

@@ -115,7 +115,7 @@ class GelfMessageFormatter extends NormalizerFormatter
         }
         }
 
 
         foreach ($extra as $key => $val) {
         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);
             $val = \is_scalar($val) || null === $val ? $val : $this->toJson($val);
             $len = \strlen($this->extraPrefix . $key . $val);
             $len = \strlen($this->extraPrefix . $key . $val);
             if ($len > $this->maxLength) {
             if ($len > $this->maxLength) {
@@ -127,7 +127,7 @@ class GelfMessageFormatter extends NormalizerFormatter
         }
         }
 
 
         foreach ($context as $key => $val) {
         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);
             $val = \is_scalar($val) || null === $val ? $val : $this->toJson($val);
             $len = \strlen($this->contextPrefix . $key . $val);
             $len = \strlen($this->contextPrefix . $key . $val);
             if ($len > $this->maxLength) {
             if ($len > $this->maxLength) {

+ 1 - 1
tests/Monolog/Formatter/GelfMessageFormatterTest.php

@@ -67,7 +67,7 @@ class GelfMessageFormatterTest extends MonologTestCase
             channel: 'meh',
             channel: 'meh',
             context: ['from' => 'logger'],
             context: ['from' => 'logger'],
             datetime: new \DateTimeImmutable("@0"),
             datetime: new \DateTimeImmutable("@0"),
-            extra: ['file' => 'test', 'line' => 14],
+            extra: ['file' => 'test', 'line' => 14, 0 => 'foo'],
         );
         );
 
 
         $message = $formatter->format($record);
         $message = $formatter->format($record);