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