TYPO3 CMS  TYPO3_7-6
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 
62  public function reset()
63  {
64  $this->loggers = [];
65  }
66 
79  public function getLogger($name = '')
80  {
82  $logger = null;
83  // Transform namespaces and underscore class names to the dot-name style
84  $separators = ['_', '\\'];
85  $name = str_replace($separators, '.', $name);
86  if (isset($this->loggers[$name])) {
87  $logger = $this->loggers[$name];
88  } else {
89  // Lazy instantiation
91  $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Logger::class, $name);
92  $this->loggers[$name] = $logger;
93  $this->setWritersForLogger($logger);
94  $this->setProcessorsForLogger($logger);
95  }
96  return $logger;
97  }
98 
105  public function registerLogger($name)
106  {
107  $this->loggers[$name] = null;
108  }
109 
115  public function getLoggerNames()
116  {
117  return array_keys($this->loggers);
118  }
119 
126  protected function setWritersForLogger(Logger $logger)
127  {
128  $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
129  foreach ($configuration as $severityLevel => $writer) {
130  foreach ($writer as $logWriterClassName => $logWriterOptions) {
132  $logWriter = null;
133  try {
134  $logWriter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
135  $logger->addWriter($severityLevel, $logWriter);
136  } catch (\Psr\Log\InvalidArgumentException $e) {
137  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
138  } catch (\TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException $e) {
139  $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
140  }
141  }
142  }
143  }
144 
151  protected function setProcessorsForLogger(Logger $logger)
152  {
153  $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->getName());
154  foreach ($configuration as $severityLevel => $processor) {
155  foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
157  $logProcessor = null;
158  try {
159  $logProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logProcessorClassName, $logProcessorOptions);
160  $logger->addProcessor($severityLevel, $logProcessor);
161  } catch (\Psr\Log\InvalidArgumentException $e) {
162  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
163  } catch (\TYPO3\CMS\Core\Log\Exception\InvalidLogProcessorConfigurationException $e) {
164  $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
165  }
166  }
167  }
168  }
169 
179  protected function getConfigurationForLogger($configurationType, $loggerName)
180  {
181  // Split up the logger name (dot-separated) into its parts
182  $explodedName = explode('.', $loggerName);
183  // Search in the $TYPO3_CONF_VARS['LOG'] array
184  // for these keys, for example "writerConfiguration"
185  $configurationKey = $configurationType . 'Configuration';
186  $configuration = $GLOBALS['TYPO3_CONF_VARS']['LOG'];
187  $result = $configuration[$configurationKey] ?: [];
188  // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
189  // and search for the most specific configuration
190  foreach ($explodedName as $partOfClassName) {
191  if (!empty($configuration[$partOfClassName][$configurationKey])) {
192  $result = $configuration[$partOfClassName][$configurationKey];
193  }
194  $configuration = $configuration[$partOfClassName];
195  }
196  // Validate the config
197  foreach ($result as $level => $unused) {
198  try {
199  LogLevel::validateLevel($level);
200  } catch (\Psr\Log\InvalidArgumentException $e) {
201  throw new \Psr\Log\InvalidArgumentException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
202  }
203  }
204  return $result;
205  }
206 }
addWriter($minimumLevel, Writer\WriterInterface $writer)
Definition: Logger.php:104
static validateLevel($level)
Definition: LogLevel.php:143
warning($message, array $data=[])
Definition: Logger.php:277
addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
Definition: Logger.php:138
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getConfigurationForLogger($configurationType, $loggerName)
Definition: LogManager.php:179