‪TYPO3CMS  11.5
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 = [];
92  foreach ($log as $logRecord) {
93  $entry = $logRecord->‪toArray();
94  // store only necessary info
95  unset($entry['data']);
96  $logArray[] = $entry;
97  }
98  return new ModuleData(
99  [
100  'levels' => $levels,
101  'startLevel' => (int)$this->‪getConfigOption('startLevel'),
102  'log' => $logArray,
103  ]
104  );
105  }
106 
110  public function ‪getSettings(): string
111  {
112  $view = GeneralUtility::makeInstance(StandaloneView::class);
113  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/LogSettings.html';
114  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
115  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
116  $view->assign('languageKey', $this->‪getBackendUser()->user['lang'] ?? null);
117 
118  $maxLevel = ‪LogLevel::normalizeLevel(LogLevel::DEBUG);
119  $levels = [];
120  for ($i = 1; $i <= $maxLevel; $i++) {
121  $levels[] = [
122  'level' => $i,
123  'levelName' => ‪LogLevel::getName($i),
124  ];
125  }
126  $view->assignMultiple(
127  [
128  'levels' => $levels,
129  'startLevel' => (int)$this->‪getConfigOption('startLevel'),
130  'groupByComponent' => $this->‪getConfigOption('groupByComponent'),
131  'groupByLevel' => $this->‪getConfigOption('groupByLevel'),
132  ]
133  );
134 
135  return $view->render();
136  }
137 
144  public function ‪getContent(ModuleData $data): string
145  {
146  $this->logLevel = (int)$this->‪getConfigOption('startLevel');
147  $view = GeneralUtility::makeInstance(StandaloneView::class);
148  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/Log.html';
149  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
150  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
151  $sortedLog = [];
152  // settings for this module
153  $groupByComponent = $this->‪getConfigOption('groupByComponent');
154  $groupByLevel = $this->‪getConfigOption('groupByLevel');
155 
156  foreach ($data['log'] as $logRecord) {
157  if (‪LogLevel::normalizeLevel($logRecord['level']) > $this->logLevel) {
158  continue;
159  }
160  if ($groupByComponent && $groupByLevel) {
161  $sortedLog[$logRecord['component']][$logRecord['level']][] = $logRecord;
162  } elseif ($groupByComponent) {
163  $sortedLog[$logRecord['component']][] = $logRecord;
164  } elseif ($groupByLevel) {
165  $sortedLog[$logRecord['level']][] = $logRecord;
166  } else {
167  $sortedLog[] = $logRecord;
168  }
169  }
170  $data['log'] = $sortedLog;
171  $data['groupByComponent'] = $groupByComponent;
172  $data['groupByLevel'] = $groupByLevel;
173  $view->assignMultiple($data->getArrayCopy());
174  $view->assign('languageKey', $this->‪getBackendUser()->user['lang'] ?? null);
175 
176  return $view->render();
177  }
178 
182  public function ‪enrich(ServerRequestInterface $request): ServerRequestInterface
183  {
184  $this->logLevel = (int)$this->‪getConfigOption('startLevel');
185 
186  // set inMemoryLogWriter recursively for all configured namespaces/areas so we don't lose log entries
187  $configWithInMemoryWriter = $this->‪setLoggingConfigRecursive(‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] ?? []);
188 
189  // in case there are empty array parts, remove them
190  ‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] = array_filter(
191  $configWithInMemoryWriter
192  );
193  return $request;
194  }
195 
196  protected function ‪setLoggingConfigRecursive(array $logConfig): array
197  {
198  foreach ($logConfig as $key => $value) {
199  if ($key === 'writerConfiguration') {
200  $logConfig[$key] = $value;
201  $logConfig[$key][LogLevel::DEBUG][InMemoryLogWriter::class] = [];
202  } elseif (is_array($value)) {
203  $logConfig[$key] = $this->‪setLoggingConfigRecursive($value);
204  }
205  }
206  return $logConfig;
207  }
208 
213  protected function ‪getConfigOption(string $option): string
214  {
215  return $this->configurationService->getConfigurationOption('debug_log', $option);
216  }
217 }
‪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:211
‪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:161
‪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:34
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getContent
‪string getContent(ModuleData $data)
Definition: Log.php:142
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log
Definition: Log.php:38
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
Definition: ModuleData.php:26
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\setLoggingConfigRecursive
‪setLoggingConfigRecursive(array $logConfig)
Definition: Log.php:194
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getSettings
‪getSettings()
Definition: Log.php:108
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\$logLevel
‪int $logLevel
Definition: Log.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪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:180
‪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:37
‪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