‪TYPO3CMS  ‪main
InMemoryLogWriter.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Log\LogLevel;
29 
38 {
40  private array ‪$log = [];
41  private bool ‪$memoryLock = false;
42 
46  public function ‪writeLog(‪LogRecord ‪$record): self
47  {
48  // Do not log if CLI, if not frontend, or memory limit has been reached.
50  || !((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface)
51  || !‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isFrontend()
52  || $this->memoryLock === true
53  ) {
54  return $this;
55  }
56 
57  // Guard: Memory Usage
59  $this->‪lockWriter();
60  return $this;
61  }
62 
63  $this->log[] = (clone ‪$record)->setMessage($this->‪interpolate($record->‪getMessage(), ‪$record->getData()));
64 
65  return $this;
66  }
67 
71  public function ‪getLogEntries(): array
72  {
73  return ‪$this->log;
74  }
75 
79  private function ‪lockWriter(): void
80  {
81  $this->memoryLock = true;
82  ‪$record = GeneralUtility::makeInstance(
83  LogRecord::class,
84  'TYPO3.CMS.AdminPanel.Log.InMemoryLogWriter',
85  LogLevel::INFO,
86  '... Further log entries omitted, memory usage too high.'
87  );
88  $this->log[] = ‪$record;
89  }
90 }
‪TYPO3\CMS\Adminpanel\Log
Definition: DoctrineSqlLogger.php:18
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter\$log
‪array $log
Definition: InMemoryLogWriter.php:40
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter\getLogEntries
‪LogRecord[] getLogEntries()
Definition: InMemoryLogWriter.php:71
‪TYPO3\CMS\Adminpanel\Utility\MemoryUtility\isMemoryConsumptionTooHigh
‪static isMemoryConsumptionTooHigh()
Definition: MemoryUtility.php:37
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:24
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter
Definition: InMemoryLogWriter.php:38
‪TYPO3\CMS\Core\Log\LogRecord\getMessage
‪string getMessage()
Definition: LogRecord.php:215
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter\writeLog
‪writeLog(LogRecord $record)
Definition: InMemoryLogWriter.php:46
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter\interpolate
‪interpolate(string $message, array $context=[])
Definition: AbstractWriter.php:49
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter
Definition: AbstractWriter.php:25
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter\$memoryLock
‪bool $memoryLock
Definition: InMemoryLogWriter.php:41
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Adminpanel\Utility\MemoryUtility
Definition: MemoryUtility.php:28
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter\lockWriter
‪lockWriter()
Definition: InMemoryLogWriter.php:79
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55