f2f9642c1cef8b6a4289c3a8cf685626d93b25e3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Log / LogManager.php
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 /**
17 * Global LogManager that keeps track of global logging information.
18 *
19 * Inspired by java.util.logging
20 *
21 * @author Ingo Renner <ingo@typo3.org>
22 * @author Steffen Müller <typo3@t3node.com>
23 * @author Steffen Gebert <steffen.gebert@typo3.org>
24 */
25 class LogManager implements \TYPO3\CMS\Core\SingletonInterface {
26
27 /**
28 * @var string
29 */
30 const CONFIGURATION_TYPE_WRITER = 'writer';
31 /**
32 * @var string
33 */
34 const CONFIGURATION_TYPE_PROCESSOR = 'processor';
35 /**
36 * Loggers to retrieve them for repeated use.
37 *
38 * @var array
39 */
40 protected $loggers = array();
41
42 /**
43 * Default / global / root logger.
44 *
45 * @var \TYPO3\CMS\Core\Log\Logger
46 */
47 protected $rootLogger = NULL;
48
49 /**
50 * Constructor
51 */
52 public function __construct() {
53 $this->rootLogger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Logger', '');
54 $this->loggers[''] = $this->rootLogger;
55 }
56
57 /**
58 * For use in unit test context only. Resets the internal logger registry.
59 *
60 * @return void
61 */
62 public function reset() {
63 $this->loggers = array();
64 }
65
66 /**
67 * Gets a logger instance for the given name.
68 *
69 * \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Log\LogManager')->getLogger('main.sub.subsub');
70 *
71 * $name can also be submitted as a underscore-separated string, which will
72 * be converted to dots. This is useful to call this method with __CLASS__
73 * as parameter.
74 *
75 * @param string $name Logger name, empty to get the global "root" logger.
76 * @return \TYPO3\CMS\Core\Log\Logger Logger with name $name
77 */
78 public function getLogger($name = '') {
79 /** @var $logger \TYPO3\CMS\Core\Log\Logger */
80 $logger = NULL;
81 // Transform namespaces and underscore class names to the dot-name style
82 $separators = array('_', '\\');
83 $name = str_replace($separators, '.', $name);
84 if (isset($this->loggers[$name])) {
85 $logger = $this->loggers[$name];
86 } else {
87 // Lazy instantiation
88 /** @var $logger \TYPO3\CMS\Core\Log\Logger */
89 $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Logger', $name);
90 $this->loggers[$name] = $logger;
91 $this->setWritersForLogger($logger);
92 $this->setProcessorsForLogger($logger);
93 }
94 return $logger;
95 }
96
97 /**
98 * For use in unit test context only.
99 *
100 * @param string $name
101 * @return void
102 */
103 public function registerLogger($name) {
104 $this->loggers[$name] = NULL;
105 }
106
107 /**
108 * For use in unit test context only.
109 *
110 * @return array
111 */
112 public function getLoggerNames() {
113 return array_keys($this->loggers);
114 }
115
116 /**
117 * Appends the writers to the given logger as configured.
118 *
119 * @param \TYPO3\CMS\Core\Log\Logger $logger Logger to configure
120 * @return void
121 * @throws \RangeException
122 */
123 protected function setWritersForLogger(\TYPO3\CMS\Core\Log\Logger $logger) {
124 $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
125 foreach ($configuration as $severityLevel => $writer) {
126 foreach ($writer as $logWriterClassName => $logWriterOptions) {
127 /** @var $logWriter \TYPO3\CMS\Core\Log\Writer\WriterInterface */
128 $logWriter = NULL;
129 try {
130 $logWriter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions);
131 $logger->addWriter($severityLevel, $logWriter);
132 } catch (\RangeException $e) {
133 $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
134 }
135 }
136 }
137 }
138
139 /**
140 * Appends the processors to the given logger as configured.
141 *
142 * @param \TYPO3\CMS\Core\Log\Logger $logger Logger to configure
143 * @return void
144 * @throws \RangeException
145 */
146 protected function setProcessorsForLogger(\TYPO3\CMS\Core\Log\Logger $logger) {
147 $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->getName());
148 foreach ($configuration as $severityLevel => $processor) {
149 foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
150 /** @var $logProcessor \TYPO3\CMS\Core\Log\Processor\ProcessorInterface */
151 $logProcessor = NULL;
152 try {
153 $logProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logProcessorClassName, $logProcessorOptions);
154 $logger->addProcessor($severityLevel, $logProcessor);
155 } catch (\RangeException $e) {
156 $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
157 }
158 }
159 }
160 }
161
162 /**
163 * Returns the configuration from $TYPO3_CONF_VARS['LOG'] as
164 * hierarchical array for different components of the class hierarchy.
165 *
166 * @param string $configurationType Type of config to return (writer, processor)
167 * @param string $loggerName Logger name
168 * @throws \RangeException
169 * @return array
170 */
171 protected function getConfigurationForLogger($configurationType, $loggerName) {
172 // Split up the logger name (dot-separated) into its parts
173 $explodedName = explode('.', $loggerName);
174 // Search in the $TYPO3_CONF_VARS['LOG'] array
175 // for these keys, for example "writerConfiguration"
176 $configurationKey = $configurationType . 'Configuration';
177 $configuration = $GLOBALS['TYPO3_CONF_VARS']['LOG'];
178 $result = $configuration[$configurationKey] ?: array();
179 // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
180 // and search for the most specific configuration
181 foreach ($explodedName as $partOfClassName) {
182 if (!empty($configuration[$partOfClassName][$configurationKey])) {
183 $result = $configuration[$partOfClassName][$configurationKey];
184 }
185 $configuration = $configuration[$partOfClassName];
186 }
187 // Validate the config
188 foreach ($result as $level => $unused) {
189 try {
190 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
191 } catch (\RangeException $e) {
192 throw new \RangeException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
193 }
194 }
195 return $result;
196 }
197
198 }