TYPO3 CMS  TYPO3_8-7
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 
23 {
27  const CONFIGURATION_TYPE_WRITER = 'writer';
28 
32  const CONFIGURATION_TYPE_PROCESSOR = 'processor';
33 
39  protected $loggers = [];
40 
46  protected $rootLogger = null;
47 
51  public function __construct()
52  {
53  $this->rootLogger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, '');
54  $this->loggers[''] = $this->rootLogger;
55  }
56 
60  public function reset()
61  {
62  $this->loggers = [];
63  }
64 
77  public function getLogger($name = '')
78  {
80  $logger = null;
81  // Transform namespaces and underscore class names to the dot-name style
82  $separators = ['_', '\\'];
83  $name = str_replace($separators, '.', $name);
84  if (isset($this->loggers[$name])) {
85  $logger = $this->loggers[$name];
86  } else {
87  // Lazy instantiation
89  $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, $name);
90  $this->loggers[$name] = $logger;
91  $this->setWritersForLogger($logger);
92  $this->setProcessorsForLogger($logger);
93  }
94  return $logger;
95  }
96 
102  public function registerLogger($name)
103  {
104  $this->loggers[$name] = null;
105  }
106 
112  public function getLoggerNames()
113  {
114  return array_keys($this->loggers);
115  }
116 
122  protected function setWritersForLogger(Logger $logger)
123  {
124  $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
125  foreach ($configuration as $severityLevel => $writer) {
126  foreach ($writer as $logWriterClassName => $logWriterOptions) {
128  $logWriter = null;
129  try {
130  $logWriter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
131  $logger->addWriter($severityLevel, $logWriter);
132  } catch (\Psr\Log\InvalidArgumentException $e) {
133  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
134  } catch (\TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException $e) {
135  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
136  }
137  }
138  }
139  }
140 
146  protected function setProcessorsForLogger(Logger $logger)
147  {
148  $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->getName());
149  foreach ($configuration as $severityLevel => $processor) {
150  foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
152  $logProcessor = null;
153  try {
154  $logProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logProcessorClassName, $logProcessorOptions);
155  $logger->addProcessor($severityLevel, $logProcessor);
156  } catch (\Psr\Log\InvalidArgumentException $e) {
157  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
158  } catch (\TYPO3\CMS\Core\Log\Exception\InvalidLogProcessorConfigurationException $e) {
159  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
160  }
161  }
162  }
163  }
164 
174  protected function getConfigurationForLogger($configurationType, $loggerName)
175  {
176  // Split up the logger name (dot-separated) into its parts
177  $explodedName = explode('.', $loggerName);
178  // Search in the $TYPO3_CONF_VARS['LOG'] array
179  // for these keys, for example "writerConfiguration"
180  $configurationKey = $configurationType . 'Configuration';
181  $configuration = $GLOBALS['TYPO3_CONF_VARS']['LOG'];
182  $result = $configuration[$configurationKey] ?? [];
183  // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
184  // and search for the most specific configuration
185  foreach ($explodedName as $partOfClassName) {
186  if (!empty($configuration[$partOfClassName][$configurationKey])) {
187  $result = $configuration[$partOfClassName][$configurationKey];
188  }
189  $configuration = $configuration[$partOfClassName];
190  }
191  // Validate the config
192  foreach ($result as $level => $unused) {
193  try {
194  LogLevel::validateLevel($level);
195  } catch (\Psr\Log\InvalidArgumentException $e) {
196  throw new \Psr\Log\InvalidArgumentException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
197  }
198  }
199  return $result;
200  }
201 }
addWriter($minimumLevel, Writer\WriterInterface $writer)
Definition: Logger.php:104
static validateLevel($level)
Definition: LogLevel.php:142
static makeInstance($className,... $constructorArguments)
warning($message, array $data=[])
Definition: Logger.php:275
addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
Definition: Logger.php:137
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getConfigurationForLogger($configurationType, $loggerName)
Definition: LogManager.php:174