‪TYPO3CMS  11.5
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 {
35  protected string ‪$name = '';
36 
40  protected string ‪$requestId = '';
41 
45  protected int ‪$minimumLogLevel;
46 
50  protected array ‪$writers = [];
51 
55  protected array ‪$processors = [];
56 
63  public function ‪__construct(string ‪$name, string ‪$requestId = '')
64  {
65  $this->name = ‪$name;
66  $this->requestId = ‪$requestId;
67  $this->minimumLogLevel = ‪LogLevel::normalizeLevel(LogLevel::EMERGENCY);
68  }
69 
73  public function ‪__wakeup()
74  {
75  $newLogger = GeneralUtility::makeInstance(LogManager::class)->getLogger($this->name);
76  $this->requestId = $newLogger->requestId;
77  $this->minimumLogLevel = $newLogger->minimumLogLevel;
78  $this->writers = $newLogger->writers;
79  $this->processors = $newLogger->processors;
80  }
81 
87  public function ‪__sleep(): array
88  {
89  return ['name'];
90  }
91 
98  protected function ‪setMinimumLogLevel($level)
99  {
101  $this->minimumLogLevel = $level;
102  return $this;
103  }
104 
110  protected function ‪getMinimumLogLevel()
111  {
113  }
114 
120  public function ‪getName()
121  {
122  return ‪$this->name;
123  }
124 
132  public function ‪addWriter(string $minimumLevel, ‪WriterInterface $writer)
133  {
134  $minLevelAsNumber = ‪LogLevel::normalizeLevel($minimumLevel);
135  // Cycle through all the log levels which are as severe as or higher
136  // than $minimumLevel and add $writer to each severity level
137  foreach (‪LogLevel::atLeast($minLevelAsNumber) as $levelName) {
138  $this->writers[$levelName] ??= [];
139  $this->writers[$levelName][] = $writer;
140  }
141  if ($minLevelAsNumber > $this->‪getMinimumLogLevel()) {
142  $this->‪setMinimumLogLevel($minLevelAsNumber);
143  }
144  return $this;
145  }
146 
152  public function ‪getWriters()
153  {
154  return ‪$this->writers;
155  }
156 
163  public function ‪addProcessor(string $minimumLevel, ‪ProcessorInterface $processor)
164  {
165  $minLevelAsNumber = ‪LogLevel::normalizeLevel($minimumLevel);
166  ‪LogLevel::validateLevel($minLevelAsNumber);
167  // Cycle through all the log levels which are as severe as or higher
168  // than $minimumLevel and add $processor to each severity level
169  for ($logLevelWhichTriggersProcessor = ‪LogLevel::normalizeLevel(LogLevel::EMERGENCY); $logLevelWhichTriggersProcessor <= $minLevelAsNumber; $logLevelWhichTriggersProcessor++) {
170  $logLevelName = ‪LogLevel::getInternalName($logLevelWhichTriggersProcessor);
171  $this->processors[$logLevelName] ??= [];
172  $this->processors[$logLevelName][] = $processor;
173  }
174  if ($minLevelAsNumber > $this->‪getMinimumLogLevel()) {
175  $this->‪setMinimumLogLevel($minLevelAsNumber);
176  }
177  }
178 
184  public function ‪getProcessors()
185  {
186  return ‪$this->processors;
187  }
188 
197  public function ‪log($level, $message, array $data = [])
198  {
199  $level = ‪LogLevel::normalizeLevel($level);
201  if ($level > $this->minimumLogLevel) {
202  return $this;
203  }
204  $record = GeneralUtility::makeInstance(LogRecord::class, $this->name, ‪LogLevel::getInternalName($level), $message, $data, $this->requestId);
205  $record = $this->‪callProcessors($record);
206  $this->‪writeLog($record);
207  return $this;
208  }
209 
217  protected function ‪callProcessors(‪LogRecord $record)
218  {
220  foreach ($this->processors[$record->‪getLevel()] ?? [] as $processor) {
221  $processedRecord = $processor->processLogRecord($record);
222  if (!$processedRecord instanceof ‪LogRecord) {
223  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
224  }
225  $record = $processedRecord;
226  }
227  return $record;
228  }
229 
235  protected function ‪writeLog(‪LogRecord $record)
236  {
238  foreach ($this->writers[$record->‪getLevel()] ?? [] as $writer) {
239  $writer->writeLog($record);
240  }
241  }
242 }
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:120
‪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:63
‪TYPO3\CMS\Core\Log
Definition: Channel.php:18
‪TYPO3\CMS\Core\Log\Logger\callProcessors
‪TYPO3 CMS Core Log LogRecord callProcessors(LogRecord $record)
Definition: Logger.php:217
‪TYPO3\CMS\Core\Log\Logger\writeLog
‪writeLog(LogRecord $record)
Definition: Logger.php:235
‪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:110
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪string getLevel()
Definition: LogRecord.php:165
‪TYPO3\CMS\Core\Log\Logger\$requestId
‪string $requestId
Definition: Logger.php:40
‪TYPO3\CMS\Core\Log\Logger\$writers
‪array $writers
Definition: Logger.php:50
‪TYPO3\CMS\Core\Log\Logger\$processors
‪array $processors
Definition: Logger.php:55
‪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:163
‪TYPO3\CMS\Core\Log\Logger\__wakeup
‪__wakeup()
Definition: Logger.php:73
‪TYPO3\CMS\Core\Log\Logger\getWriters
‪array getWriters()
Definition: Logger.php:152
‪TYPO3\CMS\Core\Log\Logger\getProcessors
‪array getProcessors()
Definition: Logger.php:184
‪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:87
‪TYPO3\CMS\Core\Log\Logger\addWriter
‪TYPO3 CMS Core Log Logger addWriter(string $minimumLevel, WriterInterface $writer)
Definition: Logger.php:132
‪TYPO3\CMS\Core\Log\Logger\$minimumLogLevel
‪int $minimumLogLevel
Definition: Logger.php:45
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Log\Logger\log
‪mixed log($level, $message, array $data=[])
Definition: Logger.php:197
‪TYPO3\CMS\Core\Log\LogLevel\atLeast
‪static array< string > atLeast($level)
Definition: LogLevel.php:112
‪TYPO3\CMS\Core\Log\Logger\setMinimumLogLevel
‪TYPO3 CMS Core Log Logger setMinimumLogLevel($level)
Definition: Logger.php:98
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int normalizeLevel($level)
Definition: LogLevel.php:94