Răsfoiți Sursa

Merge branch '2.x'

Jordi Boggiano 2 ani în urmă
părinte
comite
9b5daeaffc
4 a modificat fișierele cu 54 adăugiri și 0 ștergeri
  1. 8 0
      CHANGELOG.md
  2. 5 0
      phpstan-baseline.neon
  3. 31 0
      src/Monolog/Logger.php
  4. 10 0
      tests/Monolog/LoggerTest.php

+ 8 - 0
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)

+ 5 - 0
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

+ 31 - 0
src/Monolog/Logger.php

@@ -701,4 +701,35 @@ class Logger implements LoggerInterface, ResettableInterface
 
         ($this->exceptionHandler)($e, $record);
     }
+
+    /**
+     * @return array<string, mixed>
+     */
+    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<string, mixed> $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();
+    }
 }

+ 10 - 0
tests/Monolog/LoggerTest.php

@@ -699,6 +699,16 @@ class LoggerTest extends TestCase
         $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');