TYPO3 CMS  TYPO3_8-7
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 
137  public function addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
138  {
139  LogLevel::validateLevel($minimumLevel);
140  // Cycle through all the log levels which are as severe as or higher
141  // than $minimumLevel and add $processor to each severity level
142  for ($logLevelWhichTriggersProcessor = LogLevel::EMERGENCY; $logLevelWhichTriggersProcessor <= $minimumLevel; $logLevelWhichTriggersProcessor++) {
143  if (!isset($this->processors[$logLevelWhichTriggersProcessor])) {
144  $this->processors[$logLevelWhichTriggersProcessor] = [];
145  }
146  $this->processors[$logLevelWhichTriggersProcessor][] = $processor;
147  }
148  if ($minimumLevel > $this->getMinimumLogLevel()) {
149  $this->setMinimumLogLevel($minimumLevel);
150  }
151  }
152 
158  public function getProcessors()
159  {
160  return $this->processors;
161  }
162 
171  public function log($level, $message, array $data = [])
172  {
173  $level = LogLevel::normalizeLevel($level);
174  LogLevel::validateLevel($level);
175  if ($level > $this->minimumLogLevel) {
176  return $this;
177  }
179  $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(LogRecord::class, $this->name, $level, $message, $data);
180  $record = $this->callProcessors($record);
181  $this->writeLog($record);
182  return $this;
183  }
184 
192  protected function callProcessors(LogRecord $record)
193  {
194  if (!empty($this->processors[$record->getLevel()])) {
195  foreach ($this->processors[$record->getLevel()] as $processor) {
196  $processedRecord = $processor->processLogRecord($record);
197  if (!$processedRecord instanceof LogRecord) {
198  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
199  }
200  $record = $processedRecord;
201  }
202  }
203  return $record;
204  }
205 
211  protected function writeLog(LogRecord $record)
212  {
213  if (!empty($this->writers[$record->getLevel()])) {
214  foreach ($this->writers[$record->getLevel()] as $writer) {
215  $writer->writeLog($record);
216  }
217  }
218  }
219 
227  public function emergency($message, array $data = [])
228  {
229  return $this->log(LogLevel::EMERGENCY, $message, $data);
230  }
231 
239  public function alert($message, array $data = [])
240  {
241  return $this->log(LogLevel::ALERT, $message, $data);
242  }
243 
251  public function critical($message, array $data = [])
252  {
253  return $this->log(LogLevel::CRITICAL, $message, $data);
254  }
255 
263  public function error($message, array $data = [])
264  {
265  return $this->log(LogLevel::ERROR, $message, $data);
266  }
267 
275  public function warning($message, array $data = [])
276  {
277  return $this->log(LogLevel::WARNING, $message, $data);
278  }
279 
287  public function notice($message, array $data = [])
288  {
289  return $this->log(LogLevel::NOTICE, $message, $data);
290  }
291 
299  public function info($message, array $data = [])
300  {
301  return $this->log(LogLevel::INFO, $message, $data);
302  }
303 
311  public function debug($message, array $data = [])
312  {
313  return $this->log(LogLevel::DEBUG, $message, $data);
314  }
315 }
addWriter($minimumLevel, Writer\WriterInterface $writer)
Definition: Logger.php:104
notice($message, array $data=[])
Definition: Logger.php:287
static validateLevel($level)
Definition: LogLevel.php:142
debug($message, array $data=[])
Definition: Logger.php:311
setMinimumLogLevel($level)
Definition: Logger.php:70
static normalizeLevel($level)
Definition: LogLevel.php:155
writeLog(LogRecord $record)
Definition: Logger.php:211
alert($message, array $data=[])
Definition: Logger.php:239
emergency($message, array $data=[])
Definition: Logger.php:227
static makeInstance($className,... $constructorArguments)
info($message, array $data=[])
Definition: Logger.php:299
warning($message, array $data=[])
Definition: Logger.php:275
addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
Definition: Logger.php:137
error($message, array $data=[])
Definition: Logger.php:263
critical($message, array $data=[])
Definition: Logger.php:251
callProcessors(LogRecord $record)
Definition: Logger.php:192