[TASK] Inject requestId into LogRecords 38/55938/3
authorBenjamin Franzke <bfr@qbus.de>
Sun, 18 Feb 2018 20:11:21 +0000 (21:11 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 28 Feb 2018 16:02:45 +0000 (17:02 +0100)
Do not use global state from Bootstrap::getInstance(), rather pass
the requestId from LogManager through the Logger to the LogRecord.

We do not have a core object container yet, therefore Bootstrap now
creates a LogManager, injects the requestId and stores that instance
as singleton in the GeneralUtility singleton storage.

Change-Id: Ia9af3c3a0e029a3748045fcf73e92f874f4a37ba
Releases: master
Resolves: #84082
Reviewed-on: https://review.typo3.org/55938
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Log/LogManager.php
typo3/sysext/core/Classes/Log/LogRecord.php
typo3/sysext/core/Classes/Log/Logger.php
typo3/sysext/core/Tests/Functional/Log/Writer/DatabaseWriterTest.php

index a5ffbd8..717574b 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\Core;
 
 use Doctrine\Common\Annotations\AnnotationReader;
 use Doctrine\Common\Annotations\AnnotationRegistry;
+use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -100,22 +101,12 @@ class Bootstrap
             $applicationContext = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: 'Production');
             self::$instance = new static($applicationContext);
             self::$instance->defineTypo3RequestTypes();
+            GeneralUtility::setSingletonInstance(LogManager::class, new LogManager(self::$instance->requestId));
         }
         return static::$instance;
     }
 
     /**
-     * Gets the request's unique ID
-     *
-     * @return string Unique request ID
-     * @internal This is not a public API method, do not use in own extensions
-     */
-    public function getRequestId()
-    {
-        return $this->requestId;
-    }
-
-    /**
      * Returns the application context this bootstrap was started in.
      *
      * @return \TYPO3\CMS\Core\Core\ApplicationContext The application context encapsulated in an object
index 60f1215..131a801 100644 (file)
@@ -46,11 +46,21 @@ class LogManager implements \TYPO3\CMS\Core\SingletonInterface, LogManagerInterf
     protected $rootLogger = null;
 
     /**
+     * Unique ID of the request
+     *
+     * @var string
+     */
+    protected $requestId = '';
+
+    /**
      * Constructor
+     *
+     * @param string $requestId Unique ID of the request
      */
-    public function __construct()
+    public function __construct(string $requestId = '')
     {
-        $this->rootLogger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, '');
+        $this->requestId = $requestId;
+        $this->rootLogger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, '', $requestId);
         $this->loggers[''] = $this->rootLogger;
     }
 
@@ -86,7 +96,7 @@ class LogManager implements \TYPO3\CMS\Core\SingletonInterface, LogManagerInterf
         } else {
             // Lazy instantiation
             /** @var $logger \TYPO3\CMS\Core\Log\Logger */
-            $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, $name);
+            $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, $name, $this->requestId);
             $this->loggers[$name] = $logger;
             $this->setWritersForLogger($logger);
             $this->setProcessorsForLogger($logger);
index 2fded57..a4fc912 100644 (file)
@@ -14,8 +14,6 @@ namespace TYPO3\CMS\Core\Log;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Core\Bootstrap;
-
 /**
  * Log record
  */
@@ -95,10 +93,11 @@ class LogRecord implements \ArrayAccess
      * @param int $level Severity level (see \TYPO3\CMS\Core\Log\Level)
      * @param string $message Log message
      * @param array $data Additional data
+     * @param string $requestId Unique ID of the request
      */
-    public function __construct($component = '', $level, $message, array $data = [])
+    public function __construct($component = '', $level, $message, array $data = [], string $requestId = '')
     {
-        $this->setRequestId(Bootstrap::getInstance()->getRequestId())
+        $this->setRequestId($requestId)
             ->setCreated(microtime(true))
             ->setComponent($component)
             ->setLevel($level)
index 2bd917b..d9df05c 100644 (file)
@@ -30,6 +30,13 @@ class Logger implements \Psr\Log\LoggerInterface
     protected $name = '';
 
     /**
+     * Unique ID of the request
+     *
+     * @var string
+     */
+    protected $requestId = '';
+
+    /**
      * Minimum log level, anything below this level will be ignored.
      *
      * @var int
@@ -54,10 +61,12 @@ class Logger implements \Psr\Log\LoggerInterface
      * Constructor.
      *
      * @param string $name A name for the logger.
+     * @param string $requestId Unique ID of the request
      */
-    public function __construct($name)
+    public function __construct(string $name, string $requestId = '')
     {
         $this->name = $name;
+        $this->requestId = $requestId;
     }
 
     /**
@@ -175,7 +184,7 @@ class Logger implements \Psr\Log\LoggerInterface
             return $this;
         }
         /** @var $record \TYPO3\CMS\Core\Log\LogRecord */
-        $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(LogRecord::class, $this->name, $level, $message, $data);
+        $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(LogRecord::class, $this->name, $level, $message, $data, $this->requestId);
         $record = $this->callProcessors($record);
         $this->writeLog($record);
         return $this;
index 40a48d4..da2cd5b 100644 (file)
@@ -14,7 +14,6 @@ namespace TYPO3\CMS\Core\Tests\Functional\Log\Writer;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Core\Bootstrap;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Log\LogLevel;
 use TYPO3\CMS\Core\Log\LogRecord;
@@ -31,7 +30,7 @@ class DatabaseWriterTest extends \TYPO3\TestingFramework\Core\Functional\Functio
     public function writeLogInsertsLogRecordWithGivenProperties()
     {
         $logRecordData = [
-            'request_id' => Bootstrap::getInstance()->getRequestId(),
+            'request_id' => '5862c0e7838ac',
             'time_micro' => 1469740000.0,
             'component' => 'aComponent',
             'level' => LogLevel::DEBUG,
@@ -42,7 +41,8 @@ class DatabaseWriterTest extends \TYPO3\TestingFramework\Core\Functional\Functio
             $logRecordData['component'],
             $logRecordData['level'],
             $logRecordData['message'],
-            []
+            [],
+            $logRecordData['request_id']
         );
         $logRecord->setCreated($logRecordData['time_micro']);