[TASK] Convert log levels from string to integer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Log / Logger.php
1 <?php
2 namespace TYPO3\CMS\Core\Log;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingo Renner (ingo@typo3.org)
8 * (c) 2011-2013 Steffen Müller (typo3@t3node.com)
9 * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
10 * All rights reserved
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Logger to log events and data for different components.
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 * @author Steffen Müller <typo3@t3node.com>
34 * @author Steffen Gebert <steffen.gebert@typo3.org>
35 */
36 class Logger implements \Psr\Log\LoggerInterface {
37
38 /**
39 * Logger name or component for which this logger is meant to be used for.
40 * This should be a dot-separated name and should normally be based on
41 * the class name or the name of a subsystem, such as
42 * core.t3lib.cache.manager, core.backend.workspaces or extension.news
43 *
44 * @var string
45 */
46 protected $name = '';
47
48 /**
49 * Minimum log level, anything below this level will be ignored.
50 *
51 * @var integer
52 */
53 protected $minimumLogLevel = \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY;
54
55 /**
56 * Writers used by this logger
57 *
58 * @var array
59 */
60 protected $writers = array();
61
62 /**
63 * Processors used by this logger
64 *
65 * @var array
66 */
67 protected $processors = array();
68
69 /**
70 * Constructor.
71 *
72 * @param string $name A name for the logger.
73 * @return \TYPO3\CMS\Core\Log\Logger
74 */
75 public function __construct($name) {
76 $this->name = $name;
77 }
78
79 /**
80 * Sets the minimum log level for which log records are written.
81 *
82 * @param integer $level Minimum log level
83 * @return \TYPO3\CMS\Core\Log\Logger $this
84 */
85 protected function setMinimumLogLevel($level) {
86 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
87 $this->minimumLogLevel = $level;
88 return $this;
89 }
90
91 /**
92 * Gets the minimum log level for which log records are written.
93 *
94 * @return integer Minimum log level
95 */
96 protected function getMinimumLogLevel() {
97 return $this->minimumLogLevel;
98 }
99
100 /**
101 * Gets the logger's name.
102 *
103 * @return string Logger name.
104 */
105 public function getName() {
106 return $this->name;
107 }
108
109 /**
110 * Adds a writer to this logger
111 *
112 * @param integer $minimumLevel
113 * @param \TYPO3\CMS\Core\Log\Writer\WriterInterface $writer Writer object
114 * @return \TYPO3\CMS\Core\Log\Logger $this
115 */
116 public function addWriter($minimumLevel, \TYPO3\CMS\Core\Log\Writer\WriterInterface $writer) {
117 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($minimumLevel);
118 // Cycle through all the log levels which are as severe as or higher
119 // than $minimumLevel and add $writer to each severity level
120 for ($logLevelWhichTriggersWriter = \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY; $logLevelWhichTriggersWriter <= $minimumLevel; $logLevelWhichTriggersWriter++) {
121 if (!isset($this->writers[$logLevelWhichTriggersWriter])) {
122 $this->writers[$logLevelWhichTriggersWriter] = array();
123 }
124 $this->writers[$logLevelWhichTriggersWriter][] = $writer;
125 }
126 if ($minimumLevel > $this->getMinimumLogLevel()) {
127 $this->setMinimumLogLevel($minimumLevel);
128 }
129 return $this;
130 }
131
132 /**
133 * Returns all configured writers indexed by log level
134 *
135 * @return array
136 */
137 public function getWriters() {
138 return $this->writers;
139 }
140
141 /**
142 * Adds a processor to the logger.
143 *
144 * @param integer $minimumLevel
145 * @param \TYPO3\CMS\Core\Log\Processor\ProcessorInterface $processor The processor to add.
146 * @return void
147 */
148 public function addProcessor($minimumLevel, \TYPO3\CMS\Core\Log\Processor\ProcessorInterface $processor) {
149 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($minimumLevel);
150 // Cycle through all the log levels which are as severe as or higher
151 // than $minimumLevel and add $processor to each severity level
152 for ($logLevelWhichTriggersProcessor = \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY; $logLevelWhichTriggersProcessor <= $minimumLevel; $logLevelWhichTriggersProcessor++) {
153 if (!isset($this->processors[$logLevelWhichTriggersProcessor])) {
154 $this->processors[$logLevelWhichTriggersProcessor] = array();
155 }
156 $this->processors[$logLevelWhichTriggersProcessor][] = $processor;
157 }
158 if ($minimumLevel > $this->getMinimumLogLevel()) {
159 $this->setMinimumLogLevel($minimumLevel);
160 }
161 }
162
163 /**
164 * Returns all added processors indexed by log level
165 *
166 * @return array
167 */
168 public function getProcessors() {
169 return $this->processors;
170 }
171
172 /**
173 * Adds a log record.
174 *
175 * @param integer|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
176 * @param string $message Log message.
177 * @param array $data Additional data to log
178 * @return mixed
179 */
180 public function log($level, $message, array $data = array()) {
181 $level = LogLevel::normalizeLevel($level);
182 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
183 if ($level > $this->minimumLogLevel) {
184 return $this;
185 }
186 /** @var $record \TYPO3\CMS\Core\Log\LogRecord */
187 $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', $this->name, $level, $message, $data);
188 $record = $this->callProcessors($record);
189 $this->writeLog($record);
190 return $this;
191 }
192
193 /**
194 * Calls all processors and returns log record
195 *
196 * @param \TYPO3\CMS\Core\Log\LogRecord $record Record to process
197 * @throws \RuntimeException
198 * @return \TYPO3\CMS\Core\Log\LogRecord Processed log record
199 */
200 protected function callProcessors(\TYPO3\CMS\Core\Log\LogRecord $record) {
201 if (!empty($this->processors[$record->getLevel()])) {
202 foreach ($this->processors[$record->getLevel()] as $processor) {
203 $processedRecord = $processor->processLogRecord($record);
204 if (!$processedRecord instanceof \TYPO3\CMS\Core\Log\LogRecord) {
205 throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
206 }
207 $record = $processedRecord;
208 }
209 }
210 return $record;
211 }
212
213 /**
214 * Passes the \TYPO3\CMS\Core\Log\LogRecord to all registered writers.
215 *
216 * @param \TYPO3\CMS\Core\Log\LogRecord $record
217 * @return void
218 */
219 protected function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record) {
220 if (!empty($this->writers[$record->getLevel()])) {
221 foreach ($this->writers[$record->getLevel()] as $writer) {
222 $writer->writeLog($record);
223 }
224 }
225 }
226
227 /**
228 * Shortcut to log an EMERGENCY record.
229 *
230 * @param string $message Log message.
231 * @param array $data Additional data to log
232 * @return \TYPO3\CMS\Core\Log\Logger $this
233 */
234 public function emergency($message, array $data = array()) {
235 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::EMERGENCY, $message, $data);
236 }
237
238 /**
239 * Shortcut to log an ALERT record.
240 *
241 * @param string $message Log message.
242 * @param array $data Additional data to log
243 * @return \TYPO3\CMS\Core\Log\Logger $this
244 */
245 public function alert($message, array $data = array()) {
246 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::ALERT, $message, $data);
247 }
248
249 /**
250 * Shortcut to log a CRITICAL record.
251 *
252 * @param string $message Log message.
253 * @param array $data Additional data to log
254 * @return \TYPO3\CMS\Core\Log\Logger $this
255 */
256 public function critical($message, array $data = array()) {
257 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::CRITICAL, $message, $data);
258 }
259
260 /**
261 * Shortcut to log an ERROR record.
262 *
263 * @param string $message Log message.
264 * @param array $data Additional data to log
265 * @return \TYPO3\CMS\Core\Log\Logger $this
266 */
267 public function error($message, array $data = array()) {
268 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::ERROR, $message, $data);
269 }
270
271 /**
272 * Shortcut to log a WARNING record.
273 *
274 * @param string $message Log message.
275 * @param array $data Additional data to log
276 * @return \TYPO3\CMS\Core\Log\Logger $this
277 */
278 public function warning($message, array $data = array()) {
279 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $message, $data);
280 }
281
282 /**
283 * Shortcut to log a NOTICE record.
284 *
285 * @param string $message Log message.
286 * @param array $data Additional data to log
287 * @return \TYPO3\CMS\Core\Log\Logger $this
288 */
289 public function notice($message, array $data = array()) {
290 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::NOTICE, $message, $data);
291 }
292
293 /**
294 * Shortcut to log an INFORMATION record.
295 *
296 * @param string $message Log message.
297 * @param array $data Additional data to log
298 * @return \TYPO3\CMS\Core\Log\Logger $this
299 */
300 public function info($message, array $data = array()) {
301 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::INFO, $message, $data);
302 }
303
304 /**
305 * Shortcut to log a DEBUG record.
306 *
307 * @param string $message Log message.
308 * @param array $data Additional data to log
309 * @return \TYPO3\CMS\Core\Log\Logger $this
310 */
311 public function debug($message, array $data = array()) {
312 return $this->log(\TYPO3\CMS\Core\Log\LogLevel::DEBUG, $message, $data);
313 }
314
315 }
316
317
318 ?>