[FEATURE] Add SQL Logging to AdminPanel
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Log / InMemoryLogWriter.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Log;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Adminpanel\Utility\MemoryUtility;
20 use TYPO3\CMS\Core\Log\LogLevel;
21 use TYPO3\CMS\Core\Log\LogRecord;
22 use TYPO3\CMS\Core\Log\Writer\AbstractWriter;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Log writer that writes the log records into a static public class variable
27 * for InMemory processing
28 */
29 class InMemoryLogWriter extends AbstractWriter
30 {
31 public static $log = [];
32
33 private static $memoryLock = false;
34
35 /**
36 * Writes the log record
37 *
38 * @param LogRecord $record Log record
39 * @return self
40 * @throws \RuntimeException
41 */
42 public function writeLog(LogRecord $record): self
43 {
44 // Guard: Locked Writer
45 if (self::$memoryLock === true) {
46 return $this;
47 }
48
49 // Guard: Memory Usage
50 if (!self::$memoryLock && MemoryUtility::isMemoryConsumptionTooHigh()) {
51 $this->lockWriter();
52 return $this;
53 }
54
55 self::$log[] = $record;
56
57 return $this;
58 }
59
60 /**
61 * Lock writer and add a info message that there may potentially be more entries.
62 */
63 protected function lockWriter(): void
64 {
65 self::$memoryLock = true;
66 /** @var LogRecord $record */
67 $record = GeneralUtility::makeInstance(
68 LogRecord::class,
69 'TYPO3.CMS.AdminPanel.Log.InMemoryLogWriter',
70 LogLevel::INFO,
71 '... Further log entries omitted, memory usage too high.'
72 );
73 self::$log[] = $record;
74 }
75 }