‪TYPO3CMS  10.4
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;
23 
30 {
34  const ‪CONFIGURATION_TYPE_WRITER = 'writer';
35 
39  const ‪CONFIGURATION_TYPE_PROCESSOR = 'processor';
40 
46  protected ‪$loggers = [];
47 
53  protected ‪$rootLogger;
54 
60  protected ‪$requestId = '';
61 
67  public function ‪__construct(string ‪$requestId = '')
68  {
69  $this->requestId = ‪$requestId;
70  $this->rootLogger = GeneralUtility::makeInstance(Logger::class, '', ‪$requestId);
71  $this->loggers[''] = ‪$this->rootLogger;
72  }
73 
77  public function ‪reset()
78  {
79  $this->loggers = [];
80  }
81 
94  public function ‪getLogger($name = '')
95  {
97  $logger = null;
98  // Transform namespaces and underscore class names to the dot-name style
99  $separators = ['_', '\\'];
100  $name = str_replace($separators, '.', $name);
101  if (isset($this->loggers[$name])) {
102  $logger = $this->loggers[$name];
103  } else {
104  // Lazy instantiation
106  $logger = GeneralUtility::makeInstance(Logger::class, $name, $this->requestId);
107  $this->loggers[$name] = $logger;
108  $this->‪setWritersForLogger($logger);
109  $this->‪setProcessorsForLogger($logger);
110  }
111  return $logger;
112  }
113 
119  public function ‪registerLogger($name)
120  {
121  $this->loggers[$name] = null;
122  }
123 
129  public function ‪getLoggerNames()
130  {
131  return array_keys($this->loggers);
132  }
133 
139  protected function ‪setWritersForLogger(Logger $logger)
140  {
141  $configuration = $this->‪getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
142  foreach ($configuration as $severityLevel => $writer) {
143  foreach ($writer as $logWriterClassName => $logWriterOptions) {
144  if ($logWriterOptions['disabled'] ?? false) {
145  continue;
146  }
147  unset($logWriterOptions['disabled']);
148  try {
150  $logWriter = GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
151  $logger->addWriter($severityLevel, $logWriter);
152  } catch (InvalidArgumentException|InvalidLogWriterConfigurationException $e) {
153  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
154  }
155  }
156  }
157  }
158 
164  protected function ‪setProcessorsForLogger(Logger $logger)
165  {
166  $configuration = $this->‪getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->getName());
167  foreach ($configuration as $severityLevel => $processor) {
168  foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
169  try {
171  $logProcessor = GeneralUtility::makeInstance($logProcessorClassName, $logProcessorOptions);
172  $logger->addProcessor($severityLevel, $logProcessor);
173  } catch (InvalidArgumentException|InvalidLogProcessorConfigurationException $e) {
174  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
175  }
176  }
177  }
178  }
179 
189  protected function ‪getConfigurationForLogger($configurationType, $loggerName)
190  {
191  // Split up the logger name (dot-separated) into its parts
192  $explodedName = explode('.', $loggerName);
193  // Search in the $TYPO3_CONF_VARS['LOG'] array
194  // for these keys, for example "writerConfiguration"
195  $configurationKey = $configurationType . 'Configuration';
196  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS']['LOG'];
197  $result = $configuration[$configurationKey] ?? [];
198  // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
199  // and search for the most specific configuration
200  foreach ($explodedName as $partOfClassName) {
201  if (!isset($configuration[$partOfClassName])) {
202  break;
203  }
204  if (!empty($configuration[$partOfClassName][$configurationKey])) {
205  $result = $configuration[$partOfClassName][$configurationKey];
206  }
207  $configuration = $configuration[$partOfClassName];
208  }
209  // Validate the config
210  foreach ($result as $level => $unused) {
211  try {
213  } catch (InvalidArgumentException $e) {
214  throw new InvalidArgumentException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
215  }
216  }
217  return $result;
218  }
219 }
‪TYPO3\CMS\Core\Log\LogManager\CONFIGURATION_TYPE_WRITER
‪const CONFIGURATION_TYPE_WRITER
Definition: LogManager.php:34
‪TYPO3\CMS\Core\Log\LogManager\setProcessorsForLogger
‪setProcessorsForLogger(Logger $logger)
Definition: LogManager.php:161
‪TYPO3\CMS\Core\Log\LogManager\getConfigurationForLogger
‪array getConfigurationForLogger($configurationType, $loggerName)
Definition: LogManager.php:186
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:124
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:22
‪TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException
Definition: InvalidLogWriterConfigurationException.php:24
‪TYPO3\CMS\Core\Log
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel(int $level)
Definition: LogLevel.php:81
‪TYPO3\CMS\Core\Log\LogManager\getLogger
‪TYPO3 CMS Core Log Logger getLogger($name='')
Definition: LogManager.php:91
‪TYPO3\CMS\Core\Log\LogManager\$rootLogger
‪TYPO3 CMS Core Log Logger $rootLogger
Definition: LogManager.php:51
‪TYPO3\CMS\Core\Log\LogManager\__construct
‪__construct(string $requestId='')
Definition: LogManager.php:64
‪TYPO3\CMS\Core\Log\LogManager\$loggers
‪array $loggers
Definition: LogManager.php:45
‪TYPO3\CMS\Core\Log\Logger\addProcessor
‪addProcessor(string $minimumLevel, ProcessorInterface $processor)
Definition: Logger.php:171
‪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:136
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Log\LogManager\setWritersForLogger
‪setWritersForLogger(Logger $logger)
Definition: LogManager.php:136
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:30
‪TYPO3\CMS\Core\Log\LogManager\CONFIGURATION_TYPE_PROCESSOR
‪const CONFIGURATION_TYPE_PROCESSOR
Definition: LogManager.php:39
‪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:46
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int normalizeLevel($level)
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\Exception\InvalidLogProcessorConfigurationException
Definition: InvalidLogProcessorConfigurationException.php:24