‪TYPO3CMS  9.5
Log.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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 Psr\Http\Message\ServerRequestInterface;
30 
37 {
39 
43  protected ‪$configurationService;
44 
45  public function ‪__construct()
46  {
47  $this->configurationService = GeneralUtility::makeInstance(ConfigurationService::class);
48  }
49 
53  public function ‪getIdentifier(): string
54  {
55  return 'debug_log';
56  }
57 
63  public function ‪getLabel(): string
64  {
65  return $this->‪getLanguageService()->‪sL(
66  'LLL:EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf:submodule.log.label'
67  );
68  }
69 
73  public function ‪getDataToStore(ServerRequestInterface $request): ModuleData
74  {
75  $levels = [];
76  for ($i = 1; $i <= ‪LogLevel::DEBUG; $i++) {
77  $levels[] = [
78  'level' => $i,
79  'levelName' => ‪LogLevel::getName($i),
80  ];
81  }
82 
84 
85  $logArray = [];
87  foreach ($log as $logRecord) {
88  $entry = $logRecord->toArray();
89  // store only necessary info
90  unset($entry['data']);
91  $logArray[] = $entry;
92  }
93  return new ModuleData(
94  [
95  'levels' => $levels,
96  'startLevel' => (int)$this->‪getConfigOption('startLevel'),
97  'log' => $logArray,
98  ]
99  );
100  }
101 
105  public function ‪getSettings(): string
106  {
107  $view = GeneralUtility::makeInstance(StandaloneView::class);
108  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/LogSettings.html';
109  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
110  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
111 
112  $levels = [];
113  for ($i = 1; $i <= ‪LogLevel::DEBUG; $i++) {
114  $levels[] = [
115  'level' => $i,
116  'levelName' => ‪LogLevel::getName($i),
117  ];
118  }
119  $view->assignMultiple(
120  [
121  'levels' => $levels,
122  'startLevel' => (int)$this->‪getConfigOption('startLevel'),
123  'groupByComponent' => $this->‪getConfigOption('groupByComponent'),
124  'groupByLevel' => $this->‪getConfigOption('groupByLevel'),
125  ]
126  );
127 
128  return $view->render();
129  }
130 
137  public function ‪getContent(ModuleData $data): string
138  {
139  $this->logLevel = $this->‪getConfigOption('startLevel');
140  $view = GeneralUtility::makeInstance(StandaloneView::class);
141  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/Log.html';
142  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
143  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
144  $sortedLog = [];
145  // settings for this module
146  $groupByComponent = $this->‪getConfigOption('groupByComponent');
147  $groupByLevel = $this->‪getConfigOption('groupByLevel');
148 
149  foreach ($data['log'] as $logRecord) {
150  if ($logRecord['level'] > $this->logLevel) {
151  continue;
152  }
153  if ($groupByComponent && $groupByLevel) {
154  $sortedLog[$logRecord['component']][‪LogLevel::getName($logRecord['level'])][] = $logRecord;
155  } elseif ($groupByComponent) {
156  $sortedLog[$logRecord['component']][] = $logRecord;
157  } elseif ($groupByLevel) {
158  $sortedLog[$logRecord['level']][] = $logRecord;
159  } else {
160  $sortedLog[] = $logRecord;
161  }
162  }
163  $data['log'] = $sortedLog;
164  $data['groupByComponent'] = $groupByComponent;
165  $data['groupByLevel'] = $groupByLevel;
166  $view->assignMultiple($data->getArrayCopy());
167 
168  return $view->render();
169  }
170 
174  public function ‪initializeModule(ServerRequestInterface $request): void
175  {
176  $this->logLevel = $this->‪getConfigOption('startLevel');
177 
178  // set inMemoryLogWriter recursively for all configured namespaces/areas so we don't lose log entries
179  $configWithInMemoryWriter = $this->‪setLoggingConfigRecursive(‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] ?? []);
180 
181  // in case there are empty array parts, remove them
182  ‪$GLOBALS['TYPO3_CONF_VARS']['LOG'] = array_filter(
183  $configWithInMemoryWriter
184  );
185  }
186 
187  protected function ‪setLoggingConfigRecursive(array $logConfig): array
188  {
189  foreach ($logConfig as $key => $value) {
190  if ($key === 'writerConfiguration') {
191  $logConfig[$key] = $value;
192  $logConfig[$key][‪LogLevel::DEBUG][InMemoryLogWriter::class] = [];
193  } elseif (is_array($value)) {
194  $logConfig[$key] = $this->‪setLoggingConfigRecursive($value);
195  }
196  }
197  return $logConfig;
198  }
199 
204  protected function ‪getConfigOption(string $option): string
205  {
206  return $this->configurationService->getConfigurationOption('debug_log', $option);
207  }
208 }
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractSubModule.php:33
‪TYPO3\CMS\Core\Log\LogLevel\INFO
‪const INFO
Definition: LogLevel.php:85
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getConfigOption
‪string getConfigOption(string $option)
Definition: Log.php:203
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getLabel
‪string getLabel()
Definition: Log.php:62
‪TYPO3\CMS\Adminpanel\ModuleApi\InitializableInterface
Definition: InitializableInterface.php:34
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getIdentifier
‪string getIdentifier()
Definition: Log.php:52
‪TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface
Definition: DataProviderInterface.php:29
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getDataToStore
‪getDataToStore(ServerRequestInterface $request)
Definition: Log.php:72
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleSettingsProviderInterface
Definition: ModuleSettingsProviderInterface.php:33
‪TYPO3\CMS\Adminpanel\Modules\Debug
Definition: Log.php:4
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\$configurationService
‪ConfigurationService $configurationService
Definition: Log.php:42
‪TYPO3\CMS\Adminpanel\Service\ConfigurationService
Definition: ConfigurationService.php:33
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter
Definition: InMemoryLogWriter.php:30
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getContent
‪string getContent(ModuleData $data)
Definition: Log.php:136
‪TYPO3\CMS\Adminpanel\Log\InMemoryLogWriter\$log
‪static $log
Definition: InMemoryLogWriter.php:31
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\initializeModule
‪initializeModule(ServerRequestInterface $request)
Definition: Log.php:173
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log
Definition: Log.php:37
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\$logLevel
‪$logLevel
Definition: Log.php:38
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
Definition: ModuleData.php:26
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\setLoggingConfigRecursive
‪setLoggingConfigRecursive(array $logConfig)
Definition: Log.php:186
‪TYPO3\CMS\Core\Log\LogLevel\getName
‪static string getName($level)
Definition: LogLevel.php:117
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\getSettings
‪getSettings()
Definition: Log.php:104
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule
Definition: AbstractSubModule.php:27
‪TYPO3\CMS\Adminpanel\Modules\Debug\Log\__construct
‪__construct()
Definition: Log.php:44
‪TYPO3\CMS\Core\Log\LogLevel\DEBUG
‪const DEBUG
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:21