‪TYPO3CMS  ‪main
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\LoggerInterface;
19 use Psr\Log\LoggerTrait;
23 
27 class ‪Logger implements LoggerInterface
28 {
29  use LoggerTrait;
30 
38  protected string ‪$name = '';
39 
43  protected string ‪$requestId = '';
44 
48  protected int ‪$minimumLogLevel;
49 
53  protected array ‪$writers = [];
54 
58  protected array ‪$processors = [];
59 
66  public function ‪__construct(string ‪$name, string ‪$requestId = '')
67  {
68  $this->name = ‪$name;
69  $this->requestId = ‪$requestId;
70  $this->minimumLogLevel = ‪LogLevel::normalizeLevel(LogLevel::EMERGENCY);
71  }
72 
76  public function ‪__wakeup()
77  {
78  $newLogger = GeneralUtility::makeInstance(LogManager::class)->getLogger($this->name);
79  $this->requestId = $newLogger->requestId;
80  $this->minimumLogLevel = $newLogger->minimumLogLevel;
81  $this->writers = $newLogger->writers;
82  $this->processors = $newLogger->processors;
83  }
84 
88  public function ‪__sleep(): array
89  {
90  return ['name'];
91  }
92 
99  protected function ‪setMinimumLogLevel($level)
100  {
102  $this->minimumLogLevel = $level;
103  return $this;
104  }
105 
111  protected function ‪getMinimumLogLevel()
112  {
114  }
115 
121  public function ‪getName()
122  {
123  return ‪$this->name;
124  }
125 
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 
162  public function ‪addProcessor(string $minimumLevel, ‪ProcessorInterface $processor)
163  {
164  $minLevelAsNumber = ‪LogLevel::normalizeLevel($minimumLevel);
165  ‪LogLevel::validateLevel($minLevelAsNumber);
166  // Cycle through all the log levels which are as severe as or higher
167  // than $minimumLevel and add $processor to each severity level
168  for ($logLevelWhichTriggersProcessor = ‪LogLevel::normalizeLevel(\Psr\Log\LogLevel::EMERGENCY); $logLevelWhichTriggersProcessor <= $minLevelAsNumber; $logLevelWhichTriggersProcessor++) {
169  $logLevelName = ‪LogLevel::getInternalName($logLevelWhichTriggersProcessor);
170  $this->processors[$logLevelName] ??= [];
171  $this->processors[$logLevelName][] = $processor;
172  }
173  if ($minLevelAsNumber > $this->‪getMinimumLogLevel()) {
174  $this->‪setMinimumLogLevel($minLevelAsNumber);
175  }
176  }
177 
183  public function ‪getProcessors()
184  {
185  return ‪$this->processors;
186  }
187 
195  public function ‪log($level, string|\Stringable $message, array $data = []): void
196  {
197  $level = ‪LogLevel::normalizeLevel($level);
199  if ($level > $this->minimumLogLevel) {
200  return;
201  }
202  ‪$record = GeneralUtility::makeInstance(LogRecord::class, $this->name, ‪LogLevel::getInternalName($level), $message, $data, $this->requestId);
204  $this->‪writeLog(‪$record);
205  }
206 
215  {
217  foreach ($this->processors[‪$record->getLevel()] ?? [] as $processor) {
218  $processedRecord = $processor->processLogRecord(‪$record);
219  if (!$processedRecord instanceof ‪LogRecord) {
220  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
221  }
222  ‪$record = $processedRecord;
223  }
224  return ‪$record;
225  }
226 
230  protected function ‪writeLog(‪LogRecord ‪$record)
231  {
233  foreach ($this->writers[‪$record->getLevel()] ?? [] as $writer) {
234  $writer->writeLog(‪$record);
235  }
236  }
237 }
‪TYPO3\CMS\Core\Log\Logger\__sleep
‪__sleep()
Definition: Logger.php:88
‪TYPO3\CMS\Core\Log\Logger\getName
‪string getName()
Definition: Logger.php:121
‪TYPO3\CMS\Core\Log\Processor\ProcessorInterface
Definition: ProcessorInterface.php:27
‪TYPO3\CMS\Core\Log\Logger\$name
‪string $name
Definition: Logger.php:38
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static normalizeLevel($level)
Definition: LogLevel.php:90
‪TYPO3\CMS\Core\Log\Logger\__construct
‪__construct(string $name, string $requestId='')
Definition: Logger.php:66
‪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:214
‪TYPO3\CMS\Core\Log\Logger\writeLog
‪writeLog(LogRecord $record)
Definition: Logger.php:230
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel(int $level)
Definition: LogLevel.php:78
‪TYPO3\CMS\Core\Log\Logger\getMinimumLogLevel
‪int getMinimumLogLevel()
Definition: Logger.php:111
‪TYPO3\CMS\Core\Log\Logger\log
‪log($level, string|\Stringable $message, array $data=[])
Definition: Logger.php:195
‪TYPO3\CMS\Core\Log\Logger\$requestId
‪string $requestId
Definition: Logger.php:43
‪TYPO3\CMS\Core\Log\Logger\$writers
‪array $writers
Definition: Logger.php:53
‪TYPO3\CMS\Core\Log\Logger\$processors
‪array $processors
Definition: Logger.php:58
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:24
‪TYPO3\CMS\Core\Log\Writer\WriterInterface
Definition: WriterInterface.php:24
‪TYPO3\CMS\Core\Log\Logger\addProcessor
‪addProcessor(string $minimumLevel, ProcessorInterface $processor)
Definition: Logger.php:162
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Core\Log\Logger\__wakeup
‪__wakeup()
Definition: Logger.php:76
‪TYPO3\CMS\Core\Log\Logger\getWriters
‪array getWriters()
Definition: Logger.php:152
‪TYPO3\CMS\Core\Log\Logger\getProcessors
‪array getProcessors()
Definition: Logger.php:183
‪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:48
‪TYPO3\CMS\Core\Log\LogLevel\getInternalName
‪static getInternalName(int $level)
Definition: LogLevel.php:54
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:28
‪TYPO3\CMS\Core\Log\LogLevel\atLeast
‪static array< string > atLeast($level)
Definition: LogLevel.php:108
‪TYPO3\CMS\Core\Log\Logger\setMinimumLogLevel
‪TYPO3 CMS Core Log Logger setMinimumLogLevel($level)
Definition: Logger.php:99
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52