From 682c732a8438b42c26c7d7d9a8ce8f1a2d802950 Mon Sep 17 00:00:00 2001
From: Helmut Hummel <typo3@helhum.io>
Date: Mon, 25 Mar 2019 09:01:11 +0100
Subject: [PATCH] [BUGFIX] Fix serialization of loggers

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      | 24 +++++++++++++++++++
 .../core/Classes/Log/Writer/FileWriter.php    | 19 ---------------
 2 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/typo3/sysext/core/Classes/Log/Logger.php b/typo3/sysext/core/Classes/Log/Logger.php
index 2459957b7a22..03e85c8fdfd7 100644
--- a/typo3/sysext/core/Classes/Log/Logger.php
+++ b/typo3/sysext/core/Classes/Log/Logger.php
@@ -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.
  */
@@ -69,6 +71,28 @@ class Logger implements \Psr\Log\LoggerInterface
         $this->requestId = $requestId;
     }
 
+    /**
+     * 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.
      *
diff --git a/typo3/sysext/core/Classes/Log/Writer/FileWriter.php b/typo3/sysext/core/Classes/Log/Writer/FileWriter.php
index b068d82d2b4a..c33a1b65c23d 100644
--- a/typo3/sysext/core/Classes/Log/Writer/FileWriter.php
+++ b/typo3/sysext/core/Classes/Log/Writer/FileWriter.php
@@ -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'];
-    }
 }
-- 
GitLab