‪TYPO3CMS  9.5
LogManager.php
Go to the documentation of this file.
1 <?php
2 namespace ‪TYPO3\CMS\Core\Log;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
25 {
29  const ‪CONFIGURATION_TYPE_WRITER = 'writer';
30 
34  const ‪CONFIGURATION_TYPE_PROCESSOR = 'processor';
35 
41  protected ‪$loggers = [];
42 
48  protected ‪$rootLogger;
49 
55  protected ‪$requestId = '';
56 
62  public function ‪__construct(string ‪$requestId = '')
63  {
64  $this->requestId = ‪$requestId;
65  $this->rootLogger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, '', ‪$requestId);
66  $this->loggers[''] = ‪$this->rootLogger;
67  }
68 
72  public function ‪reset()
73  {
74  $this->loggers = [];
75  }
76 
89  public function ‪getLogger($name = '')
90  {
92  $logger = null;
93  // Transform namespaces and underscore class names to the dot-name style
94  $separators = ['_', '\\'];
95  $name = str_replace($separators, '.', $name);
96  if (isset($this->loggers[$name])) {
97  $logger = $this->loggers[$name];
98  } else {
99  // Lazy instantiation
101  $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, $name, $this->requestId);
102  $this->loggers[$name] = $logger;
103  $this->‪setWritersForLogger($logger);
104  $this->‪setProcessorsForLogger($logger);
105  }
106  return $logger;
107  }
108 
114  public function ‪registerLogger($name)
115  {
116  $this->loggers[$name] = null;
117  }
118 
124  public function ‪getLoggerNames()
125  {
126  return array_keys($this->loggers);
127  }
128 
134  protected function ‪setWritersForLogger(Logger $logger)
135  {
136  $configuration = $this->‪getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
137  foreach ($configuration as $severityLevel => $writer) {
138  foreach ($writer as $logWriterClassName => $logWriterOptions) {
139  if ($logWriterOptions['disabled'] ?? false) {
140  continue;
141  }
142  unset($logWriterOptions['disabled']);
143  try {
145  $logWriter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
146  $logger->addWriter($severityLevel, $logWriter);
147  } catch (\Psr\Log\InvalidArgumentException $e) {
148  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
149  } catch (\‪TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException $e) {
150  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
151  }
152  }
153  }
154  }
155 
161  protected function ‪setProcessorsForLogger(Logger $logger)
162  {
163  $configuration = $this->‪getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->getName());
164  foreach ($configuration as $severityLevel => $processor) {
165  foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
166  try {
168  $logProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logProcessorClassName, $logProcessorOptions);
169  $logger->addProcessor($severityLevel, $logProcessor);
170  } catch (\Psr\Log\InvalidArgumentException $e) {
171  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
172  } catch (\‪TYPO3\CMS\Core\Log\Exception\InvalidLogProcessorConfigurationException $e) {
173  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
174  }
175  }
176  }
177  }
178 
188  protected function ‪getConfigurationForLogger($configurationType, $loggerName)
189  {
190  // Split up the logger name (dot-separated) into its parts
191  $explodedName = explode('.', $loggerName);
192  // Search in the $TYPO3_CONF_VARS['LOG'] array
193  // for these keys, for example "writerConfiguration"
194  $configurationKey = $configurationType . 'Configuration';
195  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS']['LOG'];
196  $result = $configuration[$configurationKey] ?? [];
197  // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
198  // and search for the most specific configuration
199  foreach ($explodedName as $partOfClassName) {
200  if (!isset($configuration[$partOfClassName])) {
201  break;
202  }
203  if (!empty($configuration[$partOfClassName][$configurationKey])) {
204  $result = $configuration[$partOfClassName][$configurationKey];
205  }
206  $configuration = $configuration[$partOfClassName];
207  }
208  // Validate the config
209  foreach ($result as $level => $unused) {
210  try {
212  } catch (\Psr\Log\InvalidArgumentException $e) {
213  throw new \Psr\Log\InvalidArgumentException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
214  }
215  }
216  return $result;
217  }
218 }
‪TYPO3\CMS\Core\Log\Logger\addProcessor
‪addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
Definition: Logger.php:164
‪TYPO3\CMS\Core\Log\LogManager\CONFIGURATION_TYPE_WRITER
‪const CONFIGURATION_TYPE_WRITER
Definition: LogManager.php:29
‪TYPO3\CMS\Core\Log\LogManager\setProcessorsForLogger
‪setProcessorsForLogger(Logger $logger)
Definition: LogManager.php:158
‪TYPO3\CMS\Core\Log\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Log\LogManager\getConfigurationForLogger
‪array getConfigurationForLogger($configurationType, $loggerName)
Definition: LogManager.php:185
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:119
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:21
‪TYPO3
‪TYPO3\CMS\Core\Log
‪TYPO3\CMS\Core\Log\LogManager\getLogger
‪TYPO3 CMS Core Log Logger getLogger($name='')
Definition: LogManager.php:86
‪TYPO3\CMS\Core\Log\LogManager\$rootLogger
‪TYPO3 CMS Core Log Logger $rootLogger
Definition: LogManager.php:46
‪TYPO3\CMS\Core\Log\Logger\addWriter
‪TYPO3 CMS Core Log Logger addWriter($minimumLevel, Writer\WriterInterface $writer)
Definition: Logger.php:131
‪TYPO3\CMS\Core\Log\LogManager\__construct
‪__construct(string $requestId='')
Definition: LogManager.php:59
‪TYPO3\CMS\Core\Log\LogManager\$loggers
‪array $loggers
Definition: LogManager.php:40
‪TYPO3\CMS\Core\Log\LogManager\getLoggerNames
‪array getLoggerNames()
Definition: LogManager.php:121
‪TYPO3\CMS\Core\Log\LogManager\registerLogger
‪registerLogger($name)
Definition: LogManager.php:111
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$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:131
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:25
‪TYPO3\CMS\Core\Log\LogManager\CONFIGURATION_TYPE_PROCESSOR
‪const CONFIGURATION_TYPE_PROCESSOR
Definition: LogManager.php:34
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel($level)
Definition: LogLevel.php:141
‪TYPO3\CMS\Core\Log\LogManager\$requestId
‪string $requestId
Definition: LogManager.php:52
‪TYPO3\CMS\Core\Log\LogManager\reset
‪reset()
Definition: LogManager.php:69
‪TYPO3\CMS\Core\Log\Logger\warning
‪TYPO3 CMS Core Log Logger warning($message, array $data=[])
Definition: Logger.php:302