Skip to content

Commit

Permalink
Fix json formatter handling of incomplete classes, fixes #1834
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 15, 2024
1 parent 233e0b2 commit 884aa47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Monolog/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ protected function normalize($data, int $depth = 0)
return $data;
}

if (\get_class($data) === '__PHP_Incomplete_Class') {
return new \ArrayObject($data);
}

if (method_exists($data, '__toString')) {
return $data->__toString();
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Monolog/Formatter/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ public function testNormalizeHandleLargeArrays()
$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 = ['context' => ['object' => $object]];
$result = $formatter->format($record);

self::assertSame('{"context":{"object":{"__PHP_Incomplete_Class_Name":"Monolog\\\\TestClass"}}}'."\n", $result);
}

public function testEmptyContextAndExtraFieldsCanBeIgnored()
{
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);
Expand Down

0 comments on commit 884aa47

Please sign in to comment.