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