From b0080cba90f2b3634ace7de0cc3f3a1d307709c9 Mon Sep 17 00:00:00 2001
From: Helmut Hummel <typo3@helhum.io>
Date: Thu, 1 Mar 2018 15:45:16 +0100
Subject: [PATCH] [BUGFIX] Harden deprecation log handling

Because TYPO3's DefaultConfiguration is recursively
merged with settings from LocalConfiguration, it was
impossible to change or remove log writers that are
defined in DefaultConfiguration. One would have had
to use AdditionalConfiguration for that.

A simpler way to disable individual log writers is added now.
It is simpler, because it allows to disable log writers
directly in LocalConfiguration.

To avoid misconfiguration regarding deprecation logging,
E_USER_DEPRECATED is now enforced in SYS/errorHandlerErrors.

E_USER_DEPRECATED needs to be enforced to avoid,
that the default PHP error handler is called, which would cause
these errors to be added to the output or filling up log files,
depending on the global PHP configuration (php.ini).

Because all other values from DefaultConfiguration.php resemble
production settings and logging deprecation messages in production
are not desired, the deprecation logger is now disabled by default.

Last but not least, deprecation logging is now enabled/disabled in
the Live and Debug presets in the install tool UI.

Resolves: #84105
Resolves: #89934
Resolves: #88444
Releases: master, 9.5
Change-Id: I52a6f9c70ad13e6e0bad6a6b06b6fbfe7abc623c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/56077
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Benjamin Franzke <bfr@qbus.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Benjamin Franzke <bfr@qbus.de>
---
 typo3/sysext/core/Classes/Core/Bootstrap.php  |  2 +-
 typo3/sysext/core/Classes/Log/LogManager.php  |  4 ++
 .../Configuration/DefaultConfiguration.php    |  3 +-
 .../DefaultConfigurationDescription.yaml      |  2 +-
 ...84105-StreamlineDeprecationLogHandling.rst | 47 +++++++++++++++++++
 .../Configuration/Context/DebugPreset.php     |  5 +-
 .../Configuration/Context/LivePreset.php      |  5 +-
 7 files changed, 63 insertions(+), 5 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/9.5.x/Important-84105-StreamlineDeprecationLogHandling.rst

diff --git a/typo3/sysext/core/Classes/Core/Bootstrap.php b/typo3/sysext/core/Classes/Core/Bootstrap.php
index 32554cf564b6..92f335e360ef 100644
--- a/typo3/sysext/core/Classes/Core/Bootstrap.php
+++ b/typo3/sysext/core/Classes/Core/Bootstrap.php
@@ -386,7 +386,7 @@ class Bootstrap
         $debugExceptionHandlerClassName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['debugExceptionHandler'];
 
         $errorHandlerClassName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['errorHandler'];
-        $errorHandlerErrors = $GLOBALS['TYPO3_CONF_VARS']['SYS']['errorHandlerErrors'];
+        $errorHandlerErrors = $GLOBALS['TYPO3_CONF_VARS']['SYS']['errorHandlerErrors'] | E_USER_DEPRECATED;
         $exceptionalErrors = $GLOBALS['TYPO3_CONF_VARS']['SYS']['exceptionalErrors'];
 
         $displayErrorsSetting = (int)$GLOBALS['TYPO3_CONF_VARS']['SYS']['displayErrors'];
diff --git a/typo3/sysext/core/Classes/Log/LogManager.php b/typo3/sysext/core/Classes/Log/LogManager.php
index f605c6b5155e..03c21312707e 100644
--- a/typo3/sysext/core/Classes/Log/LogManager.php
+++ b/typo3/sysext/core/Classes/Log/LogManager.php
@@ -141,6 +141,10 @@ class LogManager implements SingletonInterface, LogManagerInterface
         $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
         foreach ($configuration as $severityLevel => $writer) {
             foreach ($writer as $logWriterClassName => $logWriterOptions) {
+                if ($logWriterOptions['disabled'] ?? false) {
+                    continue;
+                }
+                unset($logWriterOptions['disabled']);
                 try {
                     /** @var \TYPO3\CMS\Core\Log\Writer\WriterInterface $logWriter */
                     $logWriter = GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
diff --git a/typo3/sysext/core/Configuration/DefaultConfiguration.php b/typo3/sysext/core/Configuration/DefaultConfiguration.php
index d301c9b8a33f..8c061002b5ee 100644
--- a/typo3/sysext/core/Configuration/DefaultConfiguration.php
+++ b/typo3/sysext/core/Configuration/DefaultConfiguration.php
@@ -1426,7 +1426,8 @@ return [
                     'writerConfiguration' => [
                         \TYPO3\CMS\Core\Log\LogLevel::NOTICE => [
                             \TYPO3\CMS\Core\Log\Writer\FileWriter::class => [
-                                'logFileInfix' => 'deprecations'
+                                'logFileInfix' => 'deprecations',
+                                'disabled' => true,
                             ],
                         ]
                     ]
diff --git a/typo3/sysext/core/Configuration/DefaultConfigurationDescription.yaml b/typo3/sysext/core/Configuration/DefaultConfigurationDescription.yaml
index 1c0239371338..98e60dca1f9b 100644
--- a/typo3/sysext/core/Configuration/DefaultConfigurationDescription.yaml
+++ b/typo3/sysext/core/Configuration/DefaultConfigurationDescription.yaml
@@ -181,7 +181,7 @@ SYS:
             description: 'Classname to handle PHP errors. E.g.: TYPO3\CMS\Core\Error\ErrorHandler. This class displays and logs all errors that are registered as [SYS][errorHandlerErrors]. Leave empty to disable error handling. Errors will be logged and can be sent to the optionally installed developer log or to the "syslog" database table. If an error is registered in [SYS][exceptionalErrors] it will be turned into an exception to be handled by the configured exceptionHandler.'
         errorHandlerErrors:
             type: errors
-            description: 'The E_* constant that will be handled by the [SYS][errorHandler]. Not all PHP error types can be handled! Default is 30466 = <code>E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)</code> (see <a href="http://php.net/manual/en/errorfunc.constants.php" target="_blank" rel="noreferrer">PHP documentation</a>).'
+            description: 'The E_* constant that will be handled by the [SYS][errorHandler]. Not all PHP error types can be handled! <code>E_USER_DEPRECATED</code> will always be handled, regardless of this setting. Default is 30466 = <code>E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)</code> (see <a href="http://php.net/manual/en/errorfunc.constants.php" target="_blank" rel="noreferrer">PHP documentation</a>).'
         exceptionalErrors:
             type: errors
             description: 'The E_* constant that will be converted into an exception by the default [SYS][errorHandler]. Default is 4096 = <code>E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR | E_DEPRECATED | E_USER_DEPRECATED | E_WARNING | E_USER_ERROR | E_USER_NOTICE | E_USER_WARNING)</code> (see <a href="http://php.net/manual/en/errorfunc.constants.php" target="_blank rel="noreferrer"">PHP documentation</a>). E_USER_DEPRECATED is always excluded to avoid exceptions to be thrown for deprecation messages.'
diff --git a/typo3/sysext/core/Documentation/Changelog/9.5.x/Important-84105-StreamlineDeprecationLogHandling.rst b/typo3/sysext/core/Documentation/Changelog/9.5.x/Important-84105-StreamlineDeprecationLogHandling.rst
new file mode 100644
index 000000000000..8306612de85d
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/9.5.x/Important-84105-StreamlineDeprecationLogHandling.rst
@@ -0,0 +1,47 @@
+.. include:: ../../Includes.txt
+
+=======================================================
+Important: #84105 - Streamline deprecation log handling
+=======================================================
+
+See :issue:`84105`
+
+Description
+===========
+
+TYPO3 now comes with default configuration, in which deprecation logging is disabled.
+This means if you update to the latest TYPO3 version, you need to change your development
+configuration to enable deprecation logging in case you need it.
+
+Enabling the deprecation log can be done in Install Tool. Navigate to "Settings",
+click on "Choose Preset" in "Configuration Presets" pane, open "Debug Settings", select "Debug"
+and submit with "Activate preset".
+Disabling deprecation log can be done by selecting the "Live" preset instead.
+
+Please note, that these steps only enable/disable the FileWriter, which comes with TYPO3 default configuration.
+If you manually configured **additional** writers for the `TYPO3.CMS.deprecations` logger, you need to manually remove
+them to completely disable deprecation logging.
+
+This is how the LOG section in :file:`LocalConfiguration.php` looks like with disabled deprecation logging:
+
+.. code-block:: php
+
+   'LOG' => [
+       'TYPO3' => [
+           'CMS' => [
+               'deprecations' => [
+                   'writerConfiguration' => [
+                       \TYPO3\CMS\Core\Log\LogLevel::NOTICE => [
+                           \TYPO3\CMS\Core\Log\Writer\FileWriter::class => [
+                               'disabled' => true,
+                           ],
+                       ],
+                   ],
+               ],
+           ],
+       ],
+   ],
+
+Any other log writer can be disabled as well, by providing a `disabled` option with a truthy value.
+
+.. index:: LocalConfiguration, ext:core
diff --git a/typo3/sysext/install/Classes/Configuration/Context/DebugPreset.php b/typo3/sysext/install/Classes/Configuration/Context/DebugPreset.php
index c7141cf53a45..d703eba970ab 100644
--- a/typo3/sysext/install/Classes/Configuration/Context/DebugPreset.php
+++ b/typo3/sysext/install/Classes/Configuration/Context/DebugPreset.php
@@ -16,7 +16,8 @@
 namespace TYPO3\CMS\Install\Configuration\Context;
 
 use TYPO3\CMS\Core\Core\Environment;
-use TYPO3\CMS\Install\Configuration;
+use TYPO3\CMS\Core\Log\LogLevel;
+use TYPO3\CMS\Core\Log\Writer\FileWriter;
 use TYPO3\CMS\Install\Configuration\AbstractPreset;
 
 /**
@@ -43,6 +44,8 @@ class DebugPreset extends AbstractPreset
         'FE/debug' => true,
         'SYS/devIPmask' => '*',
         'SYS/displayErrors' => 1,
+        // Values below are not available in UI
+        'LOG/TYPO3/CMS/deprecations/writerConfiguration/' . LogLevel::NOTICE . '/' . FileWriter::class . '/disabled' => false,
         // E_WARNING | E_RECOVERABLE_ERROR | E_DEPRECATED
         'SYS/exceptionalErrors' => 12290,
     ];
diff --git a/typo3/sysext/install/Classes/Configuration/Context/LivePreset.php b/typo3/sysext/install/Classes/Configuration/Context/LivePreset.php
index 840533f8f816..85aa5fa5cf09 100644
--- a/typo3/sysext/install/Classes/Configuration/Context/LivePreset.php
+++ b/typo3/sysext/install/Classes/Configuration/Context/LivePreset.php
@@ -16,7 +16,8 @@
 namespace TYPO3\CMS\Install\Configuration\Context;
 
 use TYPO3\CMS\Core\Core\Environment;
-use TYPO3\CMS\Install\Configuration;
+use TYPO3\CMS\Core\Log\LogLevel;
+use TYPO3\CMS\Core\Log\Writer\FileWriter;
 use TYPO3\CMS\Install\Configuration\AbstractPreset;
 
 /**
@@ -43,6 +44,8 @@ class LivePreset extends AbstractPreset
         'FE/debug' => false,
         'SYS/devIPmask' => '',
         'SYS/displayErrors' => 0,
+        // Values below are not available in UI
+        'LOG/TYPO3/CMS/deprecations/writerConfiguration/' . LogLevel::NOTICE . '/' . FileWriter::class . '/disabled' => true,
         // E_RECOVERABLE_ERROR
         'SYS/exceptionalErrors' => 4096,
     ];
-- 
GitLab