[BUGFIX] Moving files in filelist renames file to "1"
[Packages/TYPO3.CMS.git] / t3lib / log / class.t3lib_log_logmanager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011-2012 Ingo Renner (ingo@typo3.org)
6 * (c) 2011-2012 Steffen Müller (typo3@t3node.com)
7 * (c) 2011-2012 Steffen Gebert (steffen.gebert@typo3.org))
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27
28
29 /**
30 * Global LogManager that keeps track of global logging information.
31 *
32 * Inspired by java.util.logging
33 *
34 * @author Ingo Renner <ingo@typo3.org>
35 * @author Steffen Müller <typo3@t3node.com>
36 * @author Steffen Gebert <steffen.gebert@typo3.org>
37 * @package TYPO3
38 * @subpackage t3lib
39 */
40 class t3lib_log_LogManager implements t3lib_Singleton {
41
42 /**
43 * @var string
44 */
45 const CONFIGURATION_TYPE_WRITER = 'writer';
46
47 /**
48 * @var string
49 */
50 const CONFIGURATION_TYPE_PROCESSOR = 'processor';
51
52 /**
53 * Loggers to retrieve them for repeated use.
54 *
55 * @var array
56 */
57 protected $loggers = array();
58
59 /**
60 * Default / global / root logger.
61 *
62 * @var t3lib_log_Logger
63 */
64 protected $rootLogger = NULL;
65
66
67 /**
68 * Constructor
69 */
70 public function __construct() {
71 $this->rootLogger = t3lib_div::makeInstance('t3lib_log_Logger', '');
72 $this->loggers[''] = $this->rootLogger;
73 }
74
75 /**
76 * For use in unit test context only. Resets the internal logger registry.
77 *
78 * @return void
79 */
80 public function reset() {
81 $this->loggers = array();
82 }
83
84 /**
85 * Gets a logger instance for the given name.
86 *
87 * t3lib_div::makeInstance('t3lib_log_LogManager')->getLogger('main.sub.subsub');
88 *
89 * $name can also be submitted as a underscore-separated string, which will
90 * be converted to dots. This is useful to call this method with __CLASS__
91 * as parameter.
92 *
93 * @param string $name Logger name, empty to get the global "root" logger.
94 * @return t3lib_log_Logger Logger with name $name
95 */
96 public function getLogger($name = '') {
97
98 /** @var $logger t3lib_log_Logger */
99 $logger = NULL;
100
101 // Transform class names to the dot-name style
102 $name = str_replace('_', '.', $name);
103
104 if (isset($this->loggers[$name])) {
105 $logger = $this->loggers[$name];
106 } else {
107
108 // Lazy instantiation
109 /** @var $logger t3lib_log_Logger */
110 $logger = t3lib_div::makeInstance('t3lib_log_Logger', $name);
111 $this->loggers[$name] = $logger;
112
113 $this->setWritersForLogger($logger);
114 $this->setProcessorsForLogger($logger);
115 }
116
117 return $logger;
118 }
119
120 /**
121 * For use in unit test context only.
122 *
123 * @param string $name
124 * @return void
125 */
126 public function registerLogger($name) {
127 $this->loggers[$name] = NULL;
128 }
129
130 /**
131 * For use in unit test context only.
132 *
133 * @return array
134 */
135 public function getLoggerNames() {
136 return array_keys($this->loggers);
137 }
138
139 /**
140 * Appends the writers to the given logger as configured.
141 *
142 * @param t3lib_log_Logger $logger Logger to configure
143 * @return void
144 * @throws RangeException
145 */
146 protected function setWritersForLogger(t3lib_log_Logger $logger) {
147 $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_WRITER, $logger->getName());
148
149 foreach ($configuration as $severityLevel => $writer) {
150 foreach ($writer as $logWriterClassName => $logWriterOptions) {
151
152 /** @var $logWriter t3lib_log_writer_Writer */
153 $logWriter = NULL;
154 try {
155 $logWriter = t3lib_div::makeInstance($logWriterClassName, $logWriterOptions);
156 $logger->addWriter($severityLevel, $logWriter);
157 } catch (RangeException $e) {
158 $logger->warning('Instantiation of LogWriter "' . $logWriterClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
159 }
160 }
161 }
162 }
163
164 /**
165 * Appends the processors to the given logger as configured.
166 *
167 * @param t3lib_log_Logger $logger Logger to configure
168 * @return void
169 * @throws RangeException
170 */
171 protected function setProcessorsForLogger(t3lib_log_Logger $logger) {
172 $configuration = $this->getConfigurationForLogger(self::CONFIGURATION_TYPE_PROCESSOR, $logger->getName());
173
174 foreach ($configuration as $severityLevel => $processor) {
175 foreach ($processor as $logProcessorClassName => $logProcessorOptions) {
176
177 /** @var $logProcessor t3lib_log_processor_Processor */
178 $logProcessor = NULL;
179 try {
180 $logProcessor = t3lib_div::makeInstance($logProcessorClassName, $logProcessorOptions);
181 $logger->addProcessor($severityLevel, $logProcessor);
182 } catch (RangeException $e) {
183 $logger->warning('Instantiation of LogProcessor "' . $logProcessorClassName . '" failed for logger ' . $logger->getName() . ' (' . $e->getMessage() . ')');
184 }
185 }
186 }
187 }
188
189 /**
190 * Returns the configuration from $TYPO3_CONF_VARS['LOG'] as
191 * hierarchical array for different components of the class hierarchy.
192 *
193 * @param string $configurationType Type of config to return (writer, processor)
194 * @param string $loggerName Logger name
195 * @throws RangeException
196 * @return array
197 */
198 protected function getConfigurationForLogger($configurationType, $loggerName) {
199
200 // Split up the logger name (dot-separated) into its parts
201 $explodedName = explode('.', $loggerName);
202
203 // Search in the $TYPO3_CONF_VARS['LOG'] array
204 // for these keys, for example "writerConfiguration"
205 $configurationKey = $configurationType . 'Configuration';
206
207 $configuration = $GLOBALS['TYPO3_CONF_VARS']['LOG'];
208
209 $result = (!empty($configuration[$configurationKey]) ? $configuration[$configurationKey] : array());
210
211 // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
212 // and search for the most specific configuration
213 foreach ($explodedName as $partOfClassName) {
214 if (!empty($configuration[$partOfClassName][$configurationKey])) {
215 $result = $configuration[$partOfClassName][$configurationKey];
216 }
217 $configuration = $configuration[$partOfClassName];
218 }
219
220 // Validate the config
221 foreach ($result as $level => $unused) {
222 try {
223 t3lib_log_Level::validateLevel($level);
224 } catch (RangeException $e) {
225 throw new RangeException(
226 'The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' .
227 $loggerName . '" is not valid.',
228 1326406447
229 );
230 }
231 }
232
233 return $result;
234 }
235 }
236
237 ?>