Skip to content

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 15, 2024
2 parents 4b18b21 + 884aa47 commit a4471eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Monolog/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ protected function normalize(mixed $data, int $depth = 0): mixed
return $data->__toString();
}

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

return $data;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Monolog/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ protected function write(LogRecord $record): void
}
$this->createDir($url);
$this->errorMessage = null;
set_error_handler([$this, 'customErrorHandler']);
set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
try {
$stream = fopen($url, 'a');
if ($this->filePermission !== null) {
Expand Down Expand Up @@ -193,7 +195,9 @@ private function createDir(string $url): void
$dir = $this->getDirFromStream($url);
if (null !== $dir && !is_dir($dir)) {
$this->errorMessage = null;
set_error_handler([$this, 'customErrorHandler']);
set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
$status = mkdir($dir, 0777, true);
restore_error_handler();
if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) {
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 @@ -274,6 +274,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 = $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()
{
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);
Expand Down

0 comments on commit a4471eb

Please sign in to comment.