‪TYPO3CMS  ‪main
Log.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;
31 
38 {
39  protected int ‪$logLevel;
40 
41  public function ‪__construct(
42  private readonly ‪ConfigurationService $configurationService,
43  ) {
44  $this->logLevel = ‪LogLevel::normalizeLevel(\Psr\‪Log\LogLevel::INFO);
45  }
46 
47  public function ‪getIdentifier(): string
48  {
49  return 'debug_log';
50  }
51 
55  public function ‪getLabel(): string
56  {
57  return $this->‪getLanguageService()->sL(
58  'LLL:EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf:submodule.log.label'
59  );
60  }
61 
62  public function ‪getDataToStore(ServerRequestInterface $request): ‪ModuleData
63  {
64  $maxLevel = ‪LogLevel::normalizeLevel(\Psr\‪Log\LogLevel::DEBUG);
65  $levels = [];
66  for ($i = 1; $i <= $maxLevel; $i++) {
67  $levels[] = [
68  'level' => $i,
69  'levelName' => ‪LogLevel::getName($i),
70  ];
71  }
72 
73  $logRecords = GeneralUtility::makeInstance(InMemoryLogWriter::class)->getLogEntries();
74 
75  $logArray = [];
76  foreach ($logRecords as $logRecord) {
77  $entry = $logRecord->toArray();
78  // store only necessary info
79  unset($entry['data']);
80  $logArray[] = $entry;
81  }
82  return new ‪ModuleData(
83  [
84  'levels' => $levels,
85  'startLevel' => (int)$this->‪getConfigOption('startLevel'),
86  'log' => $logArray,
87  ]
88  );
89  }
90 
91  public function ‪getSettings(): string
92  {
93  $view = GeneralUtility::makeInstance(StandaloneView::class);
94  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/LogSettings.html';
95  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
96  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
97  $view->assign('languageKey', $this->‪getBackendUser()->user['lang'] ?? null);
98 
99  $maxLevel = ‪LogLevel::normalizeLevel(\Psr\‪Log\LogLevel::DEBUG);
100  $levels = [];
101  for ($i = 1; $i <= $maxLevel; $i++) {
102  $levels[] = [
103  'level' => $i,
104  'levelName' => ‪LogLevel::getName($i),
105  ];
106  }
107  $view->assignMultiple(
108  [
109  'levels' => $levels,
110  'startLevel' => (int)$this->‪getConfigOption('startLevel'),
111  'groupByComponent' => $this->‪getConfigOption('groupByComponent'),
112  'groupByLevel' => $this->‪getConfigOption('groupByLevel'),
113  ]
114  );
115 
116  return $view->render();
117  }
118 
122  public function ‪getContent(‪ModuleData $data): string
123  {
124  $this->logLevel = (int)$this->‪getConfigOption('startLevel');
125  $view = GeneralUtility::makeInstance(StandaloneView::class);
126  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/Log.html';
127  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
128  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
129  $sortedLog = [];
130  // settings for this module
131  $groupByComponent = $this->‪getConfigOption('groupByComponent');
132  $groupByLevel = $this->‪getConfigOption('groupByLevel');
133 
134  foreach ($data['log'] as $logRecord) {
135  if (‪LogLevel::normalizeLevel($logRecord['level']) > $this->logLevel) {
136  continue;
137  }
138  if ($groupByComponent && $groupByLevel) {
139  $sortedLog[$logRecord['component']][$logRecord['level']][] = $logRecord;
140  } elseif ($groupByComponent) {
141  $sortedLog[$logRecord['component']][] = $logRecord;
142  } elseif ($groupByLevel) {
143  $sortedLog[$logRecord['level']][] = $logRecord;
144  } else {
145  $sortedLog[] = $logRecord;
146  }
147  }
148  $data['log'] = $sortedLog;
149  $data['groupByComponent'] = $groupByComponent;
150  $data['groupByLevel'] = $groupByLevel;
151  $view->assignMultiple($data->getArrayCopy());
152  $view->assign('languageKey', $this->‪getBackendUser()->user['lang'] ?? null);
153 
154  return $view->render();
155  }
156 
157  public function ‪enrich(ServerRequestInterface $request): ServerRequestInterface
158  {
159  $this->logLevel = (int)$this->‪getConfigOption('startLevel');
160 
161  // set inMemoryLogWriter recursively for all configured namespaces/areas so we don't lose log entries
162  $configWithInMemoryWriter = $this->‪setLoggingConfigRecursive(‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] ?? []);
163 
164  // in case there are empty array parts, remove them
165  ‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] = array_filter(
166  $configWithInMemoryWriter
167  );
168  return $request;
169  }
170 
171  protected function ‪setLoggingConfigRecursive(array $logConfig): array
172  {
173  foreach ($logConfig as $key => $value) {
174  if ($key === 'writerConfiguration') {
175  $logConfig[$key] = $value;
176  $logConfig[$key][\Psr\Log\LogLevel::DEBUG][InMemoryLogWriter::class] = [];
177  } elseif (is_array($value)) {
178  $logConfig[$key] = $this->‪setLoggingConfigRecursive($value);
179  }
180  }
181  return $logConfig;
182  }
183 
184  protected function ‪getConfigOption(string $option): string
185  {
186  return $this->configurationService->getConfigurationOption('debug_log', $option);
187  }
188 }
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getLabel
‪getLabel()
Definition: Log.php:55
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static normalizeLevel($level)
Definition: LogLevel.php:90
‪TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface
Definition: DataProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getDataToStore
‪getDataToStore(ServerRequestInterface $request)
Definition: Log.php:62
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\__construct
‪__construct(private readonly ConfigurationService $configurationService,)
Definition: Log.php:41
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleSettingsProviderInterface
Definition: ModuleSettingsProviderInterface.php:34
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getBackendUser
‪getBackendUser()
Definition: AbstractSubModule.php:35
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getContent
‪getContent(ModuleData $data)
Definition: Log.php:122
‪TYPO3\CMS\Adminpanel\Modules\Debug
Definition: Events.php:18
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getConfigOption
‪getConfigOption(string $option)
Definition: Log.php:184
‪TYPO3\CMS\Adminpanel\Service\ConfigurationService
Definition: ConfigurationService.php:34
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter
Definition: InMemoryLogWriter.php:38
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log
Definition: Log.php:38
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getLanguageService
‪getLanguageService()
Definition: AbstractSubModule.php:30
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
Definition: ModuleData.php:26
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\setLoggingConfigRecursive
‪setLoggingConfigRecursive(array $logConfig)
Definition: Log.php:171
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getSettings
‪getSettings()
Definition: Log.php:91
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\$logLevel
‪int $logLevel
Definition: Log.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\enrich
‪enrich(ServerRequestInterface $request)
Definition: Log.php:157
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule
Definition: AbstractSubModule.php:29
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getIdentifier
‪getIdentifier()
Definition: Log.php:47
‪TYPO3\CMS\Adminpanel\ModuleApi\RequestEnricherInterface
Definition: RequestEnricherInterface.php:37
‪TYPO3\CMS\Core\Log\LogLevel\getName
‪static string getName(int $level)
Definition: LogLevel.php:46