From 2ae044482cd301d95fcd1565667259f94e833e87 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 6 Feb 2023 14:33:41 +0100 Subject: [PATCH 1/4] Fix serializability of Logger class, fixes #1792 --- src/Monolog/Logger.php | 30 ++++++++++++++++++++++++++++++ tests/Monolog/LoggerTest.php | 8 +++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 775d7cf1f..5186f576b 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -722,4 +722,34 @@ protected function handleException(Throwable $e, array $record): void ($this->exceptionHandler)($e, $record); } + + public function __serialize(): array + { + return [ + 'name' => $this->name, + 'handlers' => $this->handlers, + 'processors' => $this->processors, + 'microsecondTimestamps' => $this->microsecondTimestamps, + 'timezone' => $this->timezone, + 'exceptionHandler' => $this->exceptionHandler, + 'logDepth' => $this->logDepth, + 'detectCycles' => $this->detectCycles, + ]; + } + + public function __unserialize(array $data): void + { + foreach (['name', 'handlers', 'processors', 'microsecondTimestamps', 'timezone', 'exceptionHandler', 'logDepth', 'detectCycles'] as $property) { + if (isset($data[$property])) { + $this->$property = $data; + } + } + + if (\PHP_VERSION_ID >= 80100) { + // Local variable for phpstan, see https://github.com/phpstan/phpstan/issues/6732#issuecomment-1111118412 + /** @var \WeakMap<\Fiber, int> $fiberLogDepth */ + $fiberLogDepth = new \WeakMap(); + $this->fiberLogDepth = $fiberLogDepth; + } + } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index a0ec74054..60c04a4fb 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -697,6 +697,12 @@ public function testCustomHandleException() $logger->info('test'); } + public function testSerializable() + { + $logger = new Logger(__METHOD__); + self::assertInstanceOf(Logger::class, unserialize(serialize($logger))); + } + public function testReset() { $logger = new Logger('app'); @@ -901,4 +907,4 @@ public function handleBatch(array $records): void public function close(): void { } -} \ No newline at end of file +} From 3e92e9d2437f1f9034a807571f60a36b8f2e20df Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 6 Feb 2023 14:34:37 +0100 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98d1f8b27..8a8c65124 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 2.9.1 (2023-02-06) + + * Fixed Logger not being serializable anymore (#1792) + ### 2.9.0 (2023-02-05) * Deprecated FlowdockHandler & Formatter as the flowdock service was shutdown (#1748) From c8b1e0e78928865e7fd34c9a2a6ee6272a92f8f6 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 6 Feb 2023 14:43:38 +0100 Subject: [PATCH 3/4] Fix serialization implementation --- src/Monolog/Logger.php | 2 +- tests/Monolog/LoggerTest.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 5186f576b..3fa4a1ec8 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -741,7 +741,7 @@ public function __unserialize(array $data): void { foreach (['name', 'handlers', 'processors', 'microsecondTimestamps', 'timezone', 'exceptionHandler', 'logDepth', 'detectCycles'] as $property) { if (isset($data[$property])) { - $this->$property = $data; + $this->$property = $data[$property]; } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index 60c04a4fb..8d5d6ac4d 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -700,7 +700,11 @@ public function testCustomHandleException() public function testSerializable() { $logger = new Logger(__METHOD__); - self::assertInstanceOf(Logger::class, unserialize(serialize($logger))); + $copy = unserialize(serialize($logger)); + self::assertInstanceOf(Logger::class, $copy); + self::assertSame($logger->getName(), $copy->getName()); + self::assertSame($logger->getTimezone()->getName(), $copy->getTimezone()->getName()); + self::assertSame($logger->getHandlers(), $copy->getHandlers()); } public function testReset() From f259e2b15fb95494c83f52d3caad003bbf5ffaa1 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 6 Feb 2023 14:44:46 +0100 Subject: [PATCH 4/4] Add types --- src/Monolog/Logger.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 3fa4a1ec8..84a2f5510 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -723,6 +723,9 @@ protected function handleException(Throwable $e, array $record): void ($this->exceptionHandler)($e, $record); } + /** + * @return array + */ public function __serialize(): array { return [ @@ -737,6 +740,9 @@ public function __serialize(): array ]; } + /** + * @param array $data + */ public function __unserialize(array $data): void { foreach (['name', 'handlers', 'processors', 'microsecondTimestamps', 'timezone', 'exceptionHandler', 'logDepth', 'detectCycles'] as $property) {