Browse Source

Merge branch '2.x'

Jordi Boggiano 1 year ago
parent
commit
a4471eb05b

+ 4 - 0
src/Monolog/Formatter/JsonFormatter.php

@@ -188,6 +188,10 @@ class JsonFormatter extends NormalizerFormatter
                 return $data->__toString();
                 return $data->__toString();
             }
             }
 
 
+            if (\get_class($data) === '__PHP_Incomplete_Class') {
+                return new \ArrayObject($data);
+            }
+
             return $data;
             return $data;
         }
         }
 
 

+ 6 - 2
src/Monolog/Handler/StreamHandler.php

@@ -122,7 +122,9 @@ class StreamHandler extends AbstractProcessingHandler
             }
             }
             $this->createDir($url);
             $this->createDir($url);
             $this->errorMessage = null;
             $this->errorMessage = null;
-            set_error_handler([$this, 'customErrorHandler']);
+            set_error_handler(function (...$args) {
+                return $this->customErrorHandler(...$args);
+            });
             try {
             try {
                 $stream = fopen($url, 'a');
                 $stream = fopen($url, 'a');
                 if ($this->filePermission !== null) {
                 if ($this->filePermission !== null) {
@@ -193,7 +195,9 @@ class StreamHandler extends AbstractProcessingHandler
         $dir = $this->getDirFromStream($url);
         $dir = $this->getDirFromStream($url);
         if (null !== $dir && !is_dir($dir)) {
         if (null !== $dir && !is_dir($dir)) {
             $this->errorMessage = null;
             $this->errorMessage = null;
-            set_error_handler([$this, 'customErrorHandler']);
+            set_error_handler(function (...$args) {
+                return $this->customErrorHandler(...$args);
+            });
             $status = mkdir($dir, 0777, true);
             $status = mkdir($dir, 0777, true);
             restore_error_handler();
             restore_error_handler();
             if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) {
             if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) {

+ 12 - 0
tests/Monolog/Formatter/JsonFormatterTest.php

@@ -274,6 +274,18 @@ class JsonFormatterTest extends TestCase
         $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
         $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
     }
     }
 
 
+    public function testCanNormalizeIncompleteObject(): void
+    {
+        $serialized = "O:17:\"Monolog\TestClass\":1:{s:23:\"\x00Monolog\TestClass\x00name\";s:4:\"test\";}";
+        $object = unserialize($serialized);
+
+        $formatter = new JsonFormatter();
+        $record = $this->getRecord(context: ['object' => $object], datetime: new \DateTimeImmutable('2022-02-22 00:00:00'));
+        $result = $formatter->format($record);
+
+        self::assertSame('{"message":"test","context":{"object":{"__PHP_Incomplete_Class_Name":"Monolog\\\\TestClass"}},"level":300,"level_name":"WARNING","channel":"test","datetime":"2022-02-22T00:00:00+00:00","extra":{}}'."\n", $result);
+    }
+
     public function testEmptyContextAndExtraFieldsCanBeIgnored()
     public function testEmptyContextAndExtraFieldsCanBeIgnored()
     {
     {
         $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);
         $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);