‪TYPO3CMS  9.5
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 
18 
22 class ‪Logger implements \Psr\Log\LoggerInterface
23 {
32  protected ‪$name = '';
33 
39  protected ‪$requestId = '';
40 
47 
53  protected ‪$writers = [];
54 
60  protected ‪$processors = [];
61 
68  public function ‪__construct(string ‪$name, string ‪$requestId = '')
69  {
70  $this->name = ‪$name;
71  $this->requestId = ‪$requestId;
72  }
73 
77  public function ‪__wakeup()
78  {
79  $newLogger = GeneralUtility::makeInstance(LogManager::class)->getLogger($this->name);
80  $this->requestId = $newLogger->requestId;
81  $this->minimumLogLevel = $newLogger->minimumLogLevel;
82  $this->writers = $newLogger->writers;
83  $this->processors = $newLogger->processors;
84  }
85 
91  public function ‪__sleep(): array
92  {
93  return ['name'];
94  }
95 
102  protected function ‪setMinimumLogLevel($level)
103  {
105  $this->minimumLogLevel = $level;
106  return $this;
107  }
108 
114  protected function ‪getMinimumLogLevel()
115  {
117  }
118 
124  public function ‪getName()
125  {
126  return ‪$this->name;
127  }
128 
136  public function ‪addWriter($minimumLevel, Writer\WriterInterface $writer)
137  {
138  ‪LogLevel::validateLevel($minimumLevel);
139  // Cycle through all the log levels which are as severe as or higher
140  // than $minimumLevel and add $writer to each severity level
141  for ($logLevelWhichTriggersWriter = ‪LogLevel::EMERGENCY; $logLevelWhichTriggersWriter <= $minimumLevel; $logLevelWhichTriggersWriter++) {
142  if (!isset($this->writers[$logLevelWhichTriggersWriter])) {
143  $this->writers[$logLevelWhichTriggersWriter] = [];
144  }
145  $this->writers[$logLevelWhichTriggersWriter][] = $writer;
146  }
147  if ($minimumLevel > $this->‪getMinimumLogLevel()) {
148  $this->‪setMinimumLogLevel($minimumLevel);
149  }
150  return $this;
151  }
152 
158  public function ‪getWriters()
159  {
160  return ‪$this->writers;
161  }
162 
169  public function ‪addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
170  {
171  ‪LogLevel::validateLevel($minimumLevel);
172  // Cycle through all the log levels which are as severe as or higher
173  // than $minimumLevel and add $processor to each severity level
174  for ($logLevelWhichTriggersProcessor = ‪LogLevel::EMERGENCY; $logLevelWhichTriggersProcessor <= $minimumLevel; $logLevelWhichTriggersProcessor++) {
175  if (!isset($this->processors[$logLevelWhichTriggersProcessor])) {
176  $this->processors[$logLevelWhichTriggersProcessor] = [];
177  }
178  $this->processors[$logLevelWhichTriggersProcessor][] = $processor;
179  }
180  if ($minimumLevel > $this->‪getMinimumLogLevel()) {
181  $this->‪setMinimumLogLevel($minimumLevel);
182  }
183  }
184 
190  public function ‪getProcessors()
191  {
192  return ‪$this->processors;
193  }
194 
203  public function ‪log($level, $message, array $data = [])
204  {
205  $level = ‪LogLevel::normalizeLevel($level);
207  if ($level > $this->minimumLogLevel) {
208  return $this;
209  }
211  $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(LogRecord::class, $this->name, $level, $message, $data, $this->requestId);
212  $record = $this->‪callProcessors($record);
213  $this->‪writeLog($record);
214  return $this;
215  }
216 
224  protected function ‪callProcessors(LogRecord $record)
225  {
226  if (!empty($this->processors[$record->getLevel()])) {
227  foreach ($this->processors[$record->getLevel()] as $processor) {
228  $processedRecord = $processor->processLogRecord($record);
229  if (!$processedRecord instanceof LogRecord) {
230  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
231  }
232  $record = $processedRecord;
233  }
234  }
235  return $record;
236  }
237 
243  protected function ‪writeLog(LogRecord $record)
244  {
245  if (!empty($this->writers[$record->getLevel()])) {
246  foreach ($this->writers[$record->getLevel()] as $writer) {
247  $writer->writeLog($record);
248  }
249  }
250  }
251 
259  public function ‪emergency($message, array $data = [])
260  {
261  return $this->‪log(‪LogLevel::EMERGENCY, $message, $data);
262  }
263 
271  public function ‪alert($message, array $data = [])
272  {
273  return $this->‪log(‪LogLevel::ALERT, $message, $data);
274  }
275 
283  public function ‪critical($message, array $data = [])
284  {
285  return $this->‪log(‪LogLevel::CRITICAL, $message, $data);
286  }
287 
295  public function ‪error($message, array $data = [])
296  {
297  return $this->‪log(‪LogLevel::ERROR, $message, $data);
298  }
299 
307  public function ‪warning($message, array $data = [])
308  {
309  return $this->‪log(‪LogLevel::WARNING, $message, $data);
310  }
311 
319  public function ‪notice($message, array $data = [])
320  {
321  return $this->‪log(‪LogLevel::NOTICE, $message, $data);
322  }
323 
331  public function ‪info($message, array $data = [])
332  {
333  return $this->‪log(‪LogLevel::INFO, $message, $data);
334  }
335 
343  public function ‪debug($message, array $data = [])
344  {
345  return $this->‪log(‪LogLevel::DEBUG, $message, $data);
346  }
347 }
‪TYPO3\CMS\Core\Log\Logger\addProcessor
‪addProcessor($minimumLevel, Processor\ProcessorInterface $processor)
Definition: Logger.php:164
‪TYPO3\CMS\Core\Log\LogLevel\CRITICAL
‪const CRITICAL
Definition: LogLevel.php:48
‪TYPO3\CMS\Core\Log\Logger\error
‪TYPO3 CMS Core Log Logger error($message, array $data=[])
Definition: Logger.php:290
‪TYPO3\CMS\Core\Log\LogLevel\INFO
‪const INFO
Definition: LogLevel.php:85
‪TYPO3\CMS\Core\Log\Logger\notice
‪TYPO3 CMS Core Log Logger notice($message, array $data=[])
Definition: Logger.php:314
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:119
‪TYPO3\CMS\Core\Log\Logger\$name
‪string $name
Definition: Logger.php:31
‪TYPO3\CMS\Core\Log\Logger\__construct
‪__construct(string $name, string $requestId='')
Definition: Logger.php:63
‪TYPO3\CMS\Core\Log
‪TYPO3\CMS\Core\Log\Logger\callProcessors
‪TYPO3 CMS Core Log LogRecord callProcessors(LogRecord $record)
Definition: Logger.php:219
‪TYPO3\CMS\Core\Log\Logger\writeLog
‪writeLog(LogRecord $record)
Definition: Logger.php:238
‪TYPO3\CMS\Core\Log\Logger\getMinimumLogLevel
‪int getMinimumLogLevel()
Definition: Logger.php:109
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int string normalizeLevel($level)
Definition: LogLevel.php:154
‪TYPO3\CMS\Core\Log\Logger\alert
‪TYPO3 CMS Core Log Logger alert($message, array $data=[])
Definition: Logger.php:266
‪TYPO3\CMS\Core\Log\Logger\$requestId
‪string $requestId
Definition: Logger.php:37
‪TYPO3\CMS\Core\Log\Logger\critical
‪TYPO3 CMS Core Log Logger critical($message, array $data=[])
Definition: Logger.php:278
‪TYPO3\CMS\Core\Log\Logger\addWriter
‪TYPO3 CMS Core Log Logger addWriter($minimumLevel, Writer\WriterInterface $writer)
Definition: Logger.php:131
‪TYPO3\CMS\Core\Log\LogLevel\ALERT
‪const ALERT
Definition: LogLevel.php:39
‪TYPO3\CMS\Core\Log\Logger\$writers
‪array $writers
Definition: Logger.php:49
‪TYPO3\CMS\Core\Log\Logger\$processors
‪array $processors
Definition: Logger.php:55
‪TYPO3\CMS\Core\Log\Logger\info
‪TYPO3 CMS Core Log Logger info($message, array $data=[])
Definition: Logger.php:326
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:21
‪TYPO3\CMS\Core\Log\LogLevel\ERROR
‪const ERROR
Definition: LogLevel.php:57
‪TYPO3\CMS\Core\Log\LogLevel\EMERGENCY
‪const EMERGENCY
Definition: LogLevel.php:30
‪TYPO3\CMS\Core\Log\Logger\__wakeup
‪__wakeup()
Definition: Logger.php:72
‪TYPO3\CMS\Core\Log\Logger\getWriters
‪array getWriters()
Definition: Logger.php:153
‪TYPO3\CMS\Core\Log\Logger\emergency
‪TYPO3 CMS Core Log Logger emergency($message, array $data=[])
Definition: Logger.php:254
‪TYPO3\CMS\Core\Log\Logger\getProcessors
‪array getProcessors()
Definition: Logger.php:185
‪TYPO3\CMS\Core\Log\LogLevel\WARNING
‪const WARNING
Definition: LogLevel.php:67
‪TYPO3\CMS\Core\Log\Logger\__sleep
‪array __sleep()
Definition: Logger.php:86
‪TYPO3\CMS\Core\Log\Logger\$minimumLogLevel
‪int $minimumLogLevel
Definition: Logger.php:43
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel($level)
Definition: LogLevel.php:141
‪TYPO3\CMS\Core\Log\Logger\log
‪mixed log($level, $message, array $data=[])
Definition: Logger.php:198
‪TYPO3\CMS\Core\Log\Logger\debug
‪TYPO3 CMS Core Log Logger debug($message, array $data=[])
Definition: Logger.php:338
‪TYPO3\CMS\Core\Log\Logger\setMinimumLogLevel
‪TYPO3 CMS Core Log Logger setMinimumLogLevel($level)
Definition: Logger.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪int getLevel()
Definition: LogRecord.php:164
‪TYPO3\CMS\Core\Log\Logger\warning
‪TYPO3 CMS Core Log Logger warning($message, array $data=[])
Definition: Logger.php:302
‪TYPO3\CMS\Core\Log\LogLevel\NOTICE
‪const NOTICE
Definition: LogLevel.php:76
‪TYPO3\CMS\Core\Log\LogLevel\DEBUG
‪const DEBUG
Definition: LogLevel.php:94