‪TYPO3CMS  10.4
Logger.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Log;
17 
18 use Psr\Log\AbstractLogger;
22 
26 class ‪Logger extends AbstractLogger
27 {
36  protected ‪$name = '';
37 
43  protected ‪$requestId = '';
44 
50  protected ‪$minimumLogLevel;
51 
57  protected ‪$writers = [];
58 
64  protected ‪$processors = [];
65 
72  public function ‪__construct(string ‪$name, string ‪$requestId = '')
73  {
74  $this->name = ‪$name;
75  $this->requestId = ‪$requestId;
76  $this->minimumLogLevel = ‪LogLevel::normalizeLevel(LogLevel::EMERGENCY);
77  }
78 
82  public function ‪__wakeup()
83  {
84  $newLogger = GeneralUtility::makeInstance(LogManager::class)->getLogger($this->name);
85  $this->requestId = $newLogger->requestId;
86  $this->minimumLogLevel = $newLogger->minimumLogLevel;
87  $this->writers = $newLogger->writers;
88  $this->processors = $newLogger->processors;
89  }
90 
96  public function ‪__sleep(): array
97  {
98  return ['name'];
99  }
100 
107  protected function ‪setMinimumLogLevel($level)
108  {
110  $this->minimumLogLevel = $level;
111  return $this;
112  }
113 
119  protected function ‪getMinimumLogLevel()
120  {
122  }
123 
129  public function ‪getName()
130  {
131  return ‪$this->name;
132  }
133 
141  public function ‪addWriter(string $minimumLevel, WriterInterface $writer)
142  {
143  $minLevelAsNumber = ‪LogLevel::normalizeLevel($minimumLevel);
144  ‪LogLevel::validateLevel($minLevelAsNumber);
145  // Cycle through all the log levels which are as severe as or higher
146  // than $minimumLevel and add $writer to each severity level
147  for ($logLevelWhichTriggersWriter = ‪LogLevel::normalizeLevel(LogLevel::EMERGENCY); $logLevelWhichTriggersWriter <= $minLevelAsNumber; $logLevelWhichTriggersWriter++) {
148  $logLevelName = ‪LogLevel::getInternalName($logLevelWhichTriggersWriter);
149  if (!isset($this->writers[$logLevelName])) {
150  $this->writers[$logLevelName] = [];
151  }
152  $this->writers[$logLevelName][] = $writer;
153  }
154  if ($minLevelAsNumber > $this->‪getMinimumLogLevel()) {
155  $this->‪setMinimumLogLevel($minLevelAsNumber);
156  }
157  return $this;
158  }
159 
165  public function ‪getWriters()
166  {
167  return ‪$this->writers;
168  }
169 
176  public function ‪addProcessor(string $minimumLevel, ProcessorInterface $processor)
177  {
178  $minLevelAsNumber = ‪LogLevel::normalizeLevel($minimumLevel);
179  ‪LogLevel::validateLevel($minLevelAsNumber);
180  // Cycle through all the log levels which are as severe as or higher
181  // than $minimumLevel and add $processor to each severity level
182  for ($logLevelWhichTriggersProcessor = ‪LogLevel::normalizeLevel(LogLevel::EMERGENCY); $logLevelWhichTriggersProcessor <= $minLevelAsNumber; $logLevelWhichTriggersProcessor++) {
183  $logLevelName = ‪LogLevel::getInternalName($logLevelWhichTriggersProcessor);
184  if (!isset($this->processors[$logLevelName])) {
185  $this->processors[$logLevelName] = [];
186  }
187  $this->processors[$logLevelName][] = $processor;
188  }
189  if ($minLevelAsNumber > $this->‪getMinimumLogLevel()) {
190  $this->‪setMinimumLogLevel($minLevelAsNumber);
191  }
192  }
193 
199  public function ‪getProcessors()
200  {
201  return ‪$this->processors;
202  }
203 
212  public function ‪log($level, $message, array $data = [])
213  {
214  $level = ‪LogLevel::normalizeLevel($level);
216  if ($level > $this->minimumLogLevel) {
217  return $this;
218  }
220  $record = GeneralUtility::makeInstance(LogRecord::class, $this->name, ‪LogLevel::getInternalName($level), $message, $data, $this->requestId);
221  $record = $this->‪callProcessors($record);
222  $this->‪writeLog($record);
223  return $this;
224  }
225 
233  protected function ‪callProcessors(LogRecord $record)
234  {
235  if (!empty($this->processors[$record->getLevel()])) {
237  foreach ($this->processors[$record->getLevel()] as $processor) {
238  $processedRecord = $processor->processLogRecord($record);
239  if (!$processedRecord instanceof LogRecord) {
240  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
241  }
242  $record = $processedRecord;
243  }
244  }
245  return $record;
246  }
247 
253  protected function ‪writeLog(LogRecord $record)
254  {
255  if (!empty($this->writers[$record->getLevel()])) {
257  foreach ($this->writers[$record->getLevel()] as $writer) {
258  $writer->writeLog($record);
259  }
260  }
261  }
262 }
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:124
‪TYPO3\CMS\Core\Log\Processor\ProcessorInterface
Definition: ProcessorInterface.php:27
‪TYPO3\CMS\Core\Log\Logger\$name
‪string $name
Definition: Logger.php:35
‪TYPO3\CMS\Core\Log\Logger\__construct
‪__construct(string $name, string $requestId='')
Definition: Logger.php:67
‪TYPO3\CMS\Core\Log
‪TYPO3\CMS\Core\Log\Logger\callProcessors
‪TYPO3 CMS Core Log LogRecord callProcessors(LogRecord $record)
Definition: Logger.php:228
‪TYPO3\CMS\Core\Log\Logger\writeLog
‪writeLog(LogRecord $record)
Definition: Logger.php:248
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel(int $level)
Definition: LogLevel.php:81
‪TYPO3\CMS\Core\Log\Logger\getMinimumLogLevel
‪int getMinimumLogLevel()
Definition: Logger.php:114
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪string getLevel()
Definition: LogRecord.php:165
‪TYPO3\CMS\Core\Log\Logger\$requestId
‪string $requestId
Definition: Logger.php:41
‪TYPO3\CMS\Core\Log\Logger\$writers
‪array $writers
Definition: Logger.php:53
‪TYPO3\CMS\Core\Log\Logger\$processors
‪array $processors
Definition: Logger.php:59
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:22
‪TYPO3\CMS\Core\Log\Writer\WriterInterface
Definition: WriterInterface.php:24
‪TYPO3\CMS\Core\Log\Logger\addProcessor
‪addProcessor(string $minimumLevel, ProcessorInterface $processor)
Definition: Logger.php:171
‪TYPO3\CMS\Core\Log\Logger\__wakeup
‪__wakeup()
Definition: Logger.php:77
‪TYPO3\CMS\Core\Log\Logger\getWriters
‪array getWriters()
Definition: Logger.php:160
‪TYPO3\CMS\Core\Log\Logger\getProcessors
‪array getProcessors()
Definition: Logger.php:194
‪TYPO3\CMS\Core\Log\LogLevel\getInternalName
‪static string getInternalName(int $level)
Definition: LogLevel.php:57
‪TYPO3\CMS\Core\Log\Logger\__sleep
‪array __sleep()
Definition: Logger.php:91
‪TYPO3\CMS\Core\Log\Logger\addWriter
‪TYPO3 CMS Core Log Logger addWriter(string $minimumLevel, WriterInterface $writer)
Definition: Logger.php:136
‪TYPO3\CMS\Core\Log\Logger\$minimumLogLevel
‪int $minimumLogLevel
Definition: Logger.php:47
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Log\Logger\log
‪mixed log($level, $message, array $data=[])
Definition: Logger.php:207
‪TYPO3\CMS\Core\Log\Logger\setMinimumLogLevel
‪TYPO3 CMS Core Log Logger setMinimumLogLevel($level)
Definition: Logger.php:102
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int normalizeLevel($level)
Definition: LogLevel.php:94