diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c5ffb990..c08558a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 3.3.1 (2023-02-06) + + * Fixed Logger not being serializable anymore (#1792) + ### 3.3.0 (2023-02-06) * Deprecated FlowdockHandler & Formatter as the flowdock service was shutdown (#1748) @@ -80,6 +84,10 @@ New deprecations: value equal to what `Logger::WARNING` was giving you. - `Logger::getLevelName()` is now deprecated. +### 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) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 474a5cb28..94b59539c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -90,6 +90,11 @@ parameters: count: 1 path: src/Monolog/Logger.php + - + message: "#^Variable property access on \\$this\\(Monolog\\\\Logger\\)\\.$#" + count: 1 + path: src/Monolog/Logger.php + - message: "#^Comparison operation \"\\<\" between int\\<1, 32\\> and 1 is always false\\.$#" count: 1 diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 303b11fcb..db8bc21ba 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -701,4 +701,35 @@ protected function handleException(Throwable $e, LogRecord $record): void ($this->exceptionHandler)($e, $record); } + + /** + * @return array + */ + 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, + ]; + } + + /** + * @param array $data + */ + public function __unserialize(array $data): void + { + foreach (['name', 'handlers', 'processors', 'microsecondTimestamps', 'timezone', 'exceptionHandler', 'logDepth', 'detectCycles'] as $property) { + if (isset($data[$property])) { + $this->$property = $data[$property]; + } + } + + $this->fiberLogDepth = new \WeakMap(); + } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index e5e90f642..a8a28a5e9 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -699,6 +699,16 @@ public function testCustomHandleException() $logger->info('test'); } + public function testSerializable() + { + $logger = new Logger(__METHOD__); + $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() { $logger = new Logger('app');