TYPO3 CMS  TYPO3_7-6
Logger.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 
20 class Logger implements \Psr\Log\LoggerInterface
21 {
30  protected $name = '';
31 
38 
44  protected $writers = [];
45 
51  protected $processors = [];
52 
59  public function __construct($name)
60  {
61  $this->name = $name;
62  }
63 
70  protected function setMinimumLogLevel($level)
71  {
73  $this->minimumLogLevel = $level;
74  return $this;
75  }
76 
82  protected function getMinimumLogLevel()
83  {
85  }
86 
92  public function getName()
93  {
94  return $this->name;
95  }
96 
104  public function addWriter($minimumLevel, Writer\WriterInterface $writer)
105  {
106  LogLevel::validateLevel($minimumLevel);
107  // Cycle through all the log levels which are as severe as or higher
108  // than $minimumLevel and add $writer to each severity level
109  for ($logLevelWhichTriggersWriter = LogLevel::EMERGENCY; $logLevelWhichTriggersWriter <= $minimumLevel; $logLevelWhichTriggersWriter++) {
110  if (!isset($this->writers[$logLevelWhichTriggersWriter])) {
111  $this->writers[$logLevelWhichTriggersWriter] = [];
112  }
113  $this->writers[$logLevelWhichTriggersWriter][] = $writer;
114  }
115  if ($minimumLevel > $this->getMinimumLogLevel()) {
116  $this->setMinimumLogLevel($minimumLevel);
117  }
118  return $this;
119  }
120 
126  public function getWriters()
127  {
128  return $this->writers;
129  }
130 
138  public function addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
139  {
140  LogLevel::validateLevel($minimumLevel);
141  // Cycle through all the log levels which are as severe as or higher
142  // than $minimumLevel and add $processor to each severity level
143  for ($logLevelWhichTriggersProcessor = LogLevel::EMERGENCY; $logLevelWhichTriggersProcessor <= $minimumLevel; $logLevelWhichTriggersProcessor++) {
144  if (!isset($this->processors[$logLevelWhichTriggersProcessor])) {
145  $this->processors[$logLevelWhichTriggersProcessor] = [];
146  }
147  $this->processors[$logLevelWhichTriggersProcessor][] = $processor;
148  }
149  if ($minimumLevel > $this->getMinimumLogLevel()) {
150  $this->setMinimumLogLevel($minimumLevel);
151  }
152  }
153 
159  public function getProcessors()
160  {
161  return $this->processors;
162  }
163 
172  public function log($level, $message, array $data = [])
173  {
174  $level = LogLevel::normalizeLevel($level);
175  LogLevel::validateLevel($level);
176  if ($level > $this->minimumLogLevel) {
177  return $this;
178  }
180  $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(LogRecord::class, $this->name, $level, $message, $data);
181  $record = $this->callProcessors($record);
182  $this->writeLog($record);
183  return $this;
184  }
185 
193  protected function callProcessors(LogRecord $record)
194  {
195  if (!empty($this->processors[$record->getLevel()])) {
196  foreach ($this->processors[$record->getLevel()] as $processor) {
197  $processedRecord = $processor->processLogRecord($record);
198  if (!$processedRecord instanceof LogRecord) {
199  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
200  }
201  $record = $processedRecord;
202  }
203  }
204  return $record;
205  }
206 
213  protected function writeLog(LogRecord $record)
214  {
215  if (!empty($this->writers[$record->getLevel()])) {
216  foreach ($this->writers[$record->getLevel()] as $writer) {
217  $writer->writeLog($record);
218  }
219  }
220  }
221 
229  public function emergency($message, array $data = [])
230  {
231  return $this->log(LogLevel::EMERGENCY, $message, $data);
232  }
233 
241  public function alert($message, array $data = [])
242  {
243  return $this->log(LogLevel::ALERT, $message, $data);
244  }
245 
253  public function critical($message, array $data = [])
254  {
255  return $this->log(LogLevel::CRITICAL, $message, $data);
256  }
257 
265  public function error($message, array $data = [])
266  {
267  return $this->log(LogLevel::ERROR, $message, $data);
268  }
269 
277  public function warning($message, array $data = [])
278  {
279  return $this->log(LogLevel::WARNING, $message, $data);
280  }
281 
289  public function notice($message, array $data = [])
290  {
291  return $this->log(LogLevel::NOTICE, $message, $data);
292  }
293 
301  public function info($message, array $data = [])
302  {
303  return $this->log(LogLevel::INFO, $message, $data);
304  }
305 
313  public function debug($message, array $data = [])
314  {
315  return $this->log(LogLevel::DEBUG, $message, $data);
316  }
317 }
addWriter($minimumLevel, Writer\WriterInterface $writer)
Definition: Logger.php:104
notice($message, array $data=[])
Definition: Logger.php:289
static validateLevel($level)
Definition: LogLevel.php:143
debug($message, array $data=[])
Definition: Logger.php:313
setMinimumLogLevel($level)
Definition: Logger.php:70
static normalizeLevel($level)
Definition: LogLevel.php:156
writeLog(LogRecord $record)
Definition: Logger.php:213
alert($message, array $data=[])
Definition: Logger.php:241
emergency($message, array $data=[])
Definition: Logger.php:229
info($message, array $data=[])
Definition: Logger.php:301
warning($message, array $data=[])
Definition: Logger.php:277
addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
Definition: Logger.php:138
error($message, array $data=[])
Definition: Logger.php:265
critical($message, array $data=[])
Definition: Logger.php:253
callProcessors(LogRecord $record)
Definition: Logger.php:193