Skip to content
Snippets Groups Projects
Commit c58e8da7 authored by Benjamin Franzke's avatar Benjamin Franzke Committed by Christian Kuhn
Browse files

[TASK] Inject requestId into LogRecords

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: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: default avatarFrank Naegler <frank.naegler@typo3.org>
Tested-by: default avatarFrank Naegler <frank.naegler@typo3.org>
Reviewed-by: default avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: default avatarChristian Kuhn <lolli@schwarzbu.ch>
parent 2db77448
Branches
Tags
No related merge requests found
......@@ -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,21 +101,11 @@ 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.
*
......
......@@ -45,12 +45,22 @@ 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);
......
......@@ -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)
......
......@@ -29,6 +29,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.
*
......@@ -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;
......
......@@ -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']);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment