From 233e0b27a0eb090624351f9e8080b852491d50a2 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 15 Apr 2024 10:17:33 +0200 Subject: [PATCH 1/2] Ensure private error handler does not cause problems, fixes #1866 --- src/Monolog/Handler/StreamHandler.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index 82c048e1c..561d9c755 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/src/Monolog/Handler/StreamHandler.php @@ -134,7 +134,9 @@ protected function write(array $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) { @@ -212,7 +214,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) { From 884aa47a055ea3e8e351ceaf4dfc67f52fff44c0 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 15 Apr 2024 10:56:50 +0200 Subject: [PATCH 2/2] Fix json formatter handling of incomplete classes, fixes #1834 --- src/Monolog/Formatter/JsonFormatter.php | 4 ++++ tests/Monolog/Formatter/JsonFormatterTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index b737d82e3..753e6852c 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -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(); } diff --git a/tests/Monolog/Formatter/JsonFormatterTest.php b/tests/Monolog/Formatter/JsonFormatterTest.php index 147021029..33f6b4d7d 100644 --- a/tests/Monolog/Formatter/JsonFormatterTest.php +++ b/tests/Monolog/Formatter/JsonFormatterTest.php @@ -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);