‪TYPO3CMS  11.5
LogManager.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Log;
17 
18 use Psr\Log\InvalidArgumentException;
19 use Psr\Log\LoggerInterface;
26 
33 {
37  public const ‪CONFIGURATION_TYPE_WRITER = 'writer';
38 
42  public const ‪CONFIGURATION_TYPE_PROCESSOR = 'processor';
43 
47  protected array ‪$loggers = [];
48 
53 
57  protected string ‪$requestId = '';
58 
64  public function ‪__construct(string ‪$requestId = '')
65  {
66  $this->requestId = ‪$requestId;
67  $this->rootLogger = GeneralUtility::makeInstance(Logger::class, '', ‪$requestId);
68  $this->loggers[''] = ‪$this->rootLogger;
69  }
70 
74  public function ‪reset()
75  {
76  $this->loggers = [];
77  }
78 
91  public function ‪getLogger(string $name = ''): LoggerInterface
92  {
93  // Transform namespaces and underscore class names to the dot-name style
94  $separators = ['_', '\\'];
95  $name = str_replace($separators, '.', $name);
96 
97  return $this->loggers[$name] ??= $this->‪makeLogger($name, $this->requestId);
98  }
99 
103  protected function ‪makeLogger(string $name, string ‪$requestId): ‪Logger
104  {
105  $logger = GeneralUtility::makeInstance(Logger::class, $name, ‪$requestId);
106  $this->‪setWritersForLogger($logger);
107  $this->‪setProcessorsForLogger($logger);
108  return $logger;
109  }
110 
116  public function ‪registerLogger($name)
117  {
118  $this->loggers[$name] = null;
119  }
120 
126  public function ‪getLoggerNames()
127  {
128  return array_keys($this->loggers);
129  }
130 
136  protected function ‪setWritersForLogger(‪Logger $logger)
137  {
138  $configuration = $this->‪getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->‪getName());
139  foreach ($configuration as $severityLevel => $writer) {
140  $writer = array_filter($writer, static fn(array $options) => !($options['disabled'] ?? false));
141  foreach ($writer as $logWriterClassName => $logWriterOptions) {
142  try {
143  unset($logWriterOptions['disabled']);
145  $logWriter = GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
146  $logger->‪addWriter($severityLevel, $logWriter);
147  } catch (InvalidArgumentException|‪InvalidLogWriterConfigurationException $e) {
148  $logger->warning('Instantiation of LogWriter "{class_name}" failed for logger {name}', [
149  'class_name' => $logWriterClassName,
150  'name' => $logger->‪getName(),
151  'exception' => $e,
152  ]);
153  }
154  }
155  }
156  }
157 
163  protected function ‪setProcessorsForLogger(‪Logger $logger)
164  {
165  $configuration = $this->‪getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->‪getName());
166  foreach ($configuration as $severityLevel => $processor) {
167  foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
168  try {
170  $logProcessor = GeneralUtility::makeInstance($logProcessorClassName, $logProcessorOptions);
171  $logger->‪addProcessor($severityLevel, $logProcessor);
172  } catch (InvalidArgumentException|‪InvalidLogProcessorConfigurationException $e) {
173  $logger->warning('Instantiation of LogProcessor "{class_name}" failed for logger {name}', [
174  'class_name' => $logProcessorClassName,
175  'name' => $logger->‪getName(),
176  'exception' => $e,
177  ]);
178  }
179  }
180  }
181  }
182 
192  protected function ‪getConfigurationForLogger($configurationType, $loggerName)
193  {
194  // Split up the logger name (dot-separated) into its parts
195  $explodedName = explode('.', $loggerName);
196  // Search in the $TYPO3_CONF_VARS['LOG'] array
197  // for these keys, for example "writerConfiguration"
198  $configurationKey = $configurationType . 'Configuration';
199  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] ?? [];
200  $result = $configuration[$configurationKey] ?? [];
201  // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
202  // and search for the most specific configuration
203  foreach ($explodedName as $partOfClassName) {
204  if (!isset($configuration[$partOfClassName])) {
205  break;
206  }
207  if (!empty($configuration[$partOfClassName][$configurationKey])) {
208  $result = $configuration[$partOfClassName][$configurationKey];
209  }
210  $configuration = $configuration[$partOfClassName];
211  }
212  // Validate the config
213  foreach ($result as $level => $unused) {
214  try {
216  } catch (InvalidArgumentException $e) {
217  throw new InvalidArgumentException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
218  }
219  }
220  return $result;
221  }
222 }
‪TYPO3\CMS\Core\Log\LogManager\CONFIGURATION_TYPE_WRITER
‪const CONFIGURATION_TYPE_WRITER
Definition: LogManager.php:37
‪TYPO3\CMS\Core\Log\LogManager\setProcessorsForLogger
‪setProcessorsForLogger(Logger $logger)
Definition: LogManager.php:163
‪TYPO3\CMS\Core\Log\LogManager\getConfigurationForLogger
‪array getConfigurationForLogger($configurationType, $loggerName)
Definition: LogManager.php:192
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:120
‪TYPO3\CMS\Core\Log\Processor\ProcessorInterface
Definition: ProcessorInterface.php:27
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:24
‪TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException
Definition: InvalidLogWriterConfigurationException.php:23
‪TYPO3\CMS\Core\Log
Definition: Channel.php:18
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel(int $level)
Definition: LogLevel.php:81
‪TYPO3\CMS\Core\Log\LogManager\__construct
‪__construct(string $requestId='')
Definition: LogManager.php:64
‪TYPO3\CMS\Core\Log\LogManager\$rootLogger
‪Logger $rootLogger
Definition: LogManager.php:52
‪TYPO3\CMS\Core\Log\Writer\WriterInterface
Definition: WriterInterface.php:24
‪TYPO3\CMS\Core\Log\LogManager\$loggers
‪array $loggers
Definition: LogManager.php:47
‪TYPO3\CMS\Core\Log\Logger\addProcessor
‪addProcessor(string $minimumLevel, ProcessorInterface $processor)
Definition: Logger.php:163
‪TYPO3\CMS\Core\Log\LogManager\getLogger
‪Logger getLogger(string $name='')
Definition: LogManager.php:91
‪TYPO3\CMS\Core\Log\LogManager\getLoggerNames
‪array getLoggerNames()
Definition: LogManager.php:126
‪TYPO3\CMS\Core\Log\LogManager\registerLogger
‪registerLogger($name)
Definition: LogManager.php:116
‪TYPO3\CMS\Core\Log\Logger\addWriter
‪TYPO3 CMS Core Log Logger addWriter(string $minimumLevel, WriterInterface $writer)
Definition: Logger.php:132
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Log\LogManager\setWritersForLogger
‪setWritersForLogger(Logger $logger)
Definition: LogManager.php:136
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Log\LogManager\CONFIGURATION_TYPE_PROCESSOR
‪const CONFIGURATION_TYPE_PROCESSOR
Definition: LogManager.php:42
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Log\LogManager\$requestId
‪string $requestId
Definition: LogManager.php:57
‪TYPO3\CMS\Core\Log\LogManager\reset
‪reset()
Definition: LogManager.php:74
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int normalizeLevel($level)
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\LogManager\makeLogger
‪makeLogger(string $name, string $requestId)
Definition: LogManager.php:103
‪TYPO3\CMS\Core\Log\Exception\InvalidLogProcessorConfigurationException
Definition: InvalidLogProcessorConfigurationException.php:23