[BUGFIX] Fix serialization of loggers 25/60325/2
authorHelmut Hummel <typo3@helhum.io>
Mon, 25 Mar 2019 08:01:11 +0000 (09:01 +0100)
committerSusanne Moog <look@susi.dev>
Sun, 14 Apr 2019 15:57:56 +0000 (17:57 +0200)
Only dealing with PHP file resource in writers is not
enough to properly initialize a logger on wakeup.

A logger has to log with a new request IDs and possibly completely new writers
and processors due to configuration changes in the meantime.

Therefore the __sleep and __wakeup methods are removed in the FileWriter
and new methods are added to the logger instance to make sure a
wakeup will have a correctly configured logger instance.

Resolves: #86941
Resolves: #87261
Releases: master, 9.5
Change-Id: Ia36e251404eae2bdf0dfdf52ace7dcf1815ec456
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/60325
Tested-by: Ralf Merz <mail@merzilla.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Reviewed-by: Ralf Merz <mail@merzilla.de>
Reviewed-by: Susanne Moog <look@susi.dev>
typo3/sysext/core/Classes/Log/Logger.php
typo3/sysext/core/Classes/Log/Writer/FileWriter.php

index 2459957..03e85c8 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Core\Log;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Logger to log events and data for different components.
  */
@@ -70,6 +72,28 @@ class Logger implements \Psr\Log\LoggerInterface
     }
 
     /**
+     * Re-initialize instance with creating a new instance with up to date information
+     */
+    public function __wakeup()
+    {
+        $newLogger = GeneralUtility::makeInstance(LogManager::class)->getLogger($this->name);
+        $this->requestId = $newLogger->requestId;
+        $this->minimumLogLevel = $newLogger->minimumLogLevel;
+        $this->writers = $newLogger->writers;
+        $this->processors = $newLogger->processors;
+    }
+
+    /**
+     * Remove everything except the name, to be able to restore it on wakeup
+     *
+     * @return array
+     */
+    public function __sleep(): array
+    {
+        return ['name'];
+    }
+
+    /**
      * Sets the minimum log level for which log records are written.
      *
      * @param int $level Minimum log level
index b068d82..c33a1b6 100644 (file)
@@ -271,23 +271,4 @@ class FileWriter extends AbstractWriter
         }
         return Environment::getVarPath() . sprintf($this->defaultLogFileTemplate, $namePart);
     }
-
-    /**
-     * Allow serialization of logger - reinitialize log file on unserializing
-     */
-    public function __wakeup()
-    {
-        self::$logFileHandlesCount[$this->logFile]++;
-        $this->setLogFile($this->logFile ?: $this->getDefaultLogFileName());
-    }
-
-    /**
-     * Property 'logFile' should be kept
-     *
-     * @return array
-     */
-    public function __sleep(): array
-    {
-        return ['logFile'];
-    }
 }