‪TYPO3CMS  ‪main
ListSysLogCommand.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Symfony\Component\Console\Command\Command;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Output\OutputInterface;
21 use Symfony\Component\Console\Style\SymfonyStyle;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 
32 class ‪ListSysLogCommand extends Command
33 {
34  use ‪LogDataTrait;
35 
39  public function ‪configure()
40  {
41  $this->setHelp('Prints a list of recent sys_log entries.' . LF . 'If you want to get more detailed information, use the --verbose option.');
42  }
43 
47  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
48  {
49  $io = new SymfonyStyle($input, ‪$output);
50  $io->title($this->getDescription());
51  $showDetails = ‪$output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL;
52 
53  $tableHeaders = [
54  'Log ID',
55  'Date & Time',
56  'User ID',
57  'Message',
58  ];
59  if ($showDetails) {
60  $tableHeaders[] = 'Details';
61  }
62 
63  // Initialize result array
64  $content = [];
65 
66  // Select DB relations from reference table
67  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
68  $rowIterator = $queryBuilder
69  ->select('*')
70  ->from('sys_log')
71  ->where(
72  $queryBuilder->expr()->gt(
73  'tstamp',
74  $queryBuilder->createNamedParameter(‪$GLOBALS['EXEC_TIME'] - 24 * 3600, ‪Connection::PARAM_INT)
75  )
76  )
77  ->orderBy('tstamp', 'DESC')
78  ->executeQuery();
79 
80  while ($row = $rowIterator->fetchAssociative()) {
81  $logData = $this->‪unserializeLogData($row['log_data'] ?? '');
82  $text = $this->‪formatLogDetails($row['details'] ?? '', $logData);
83  $userInformation = $row['userid'];
84  if (!empty($logData['originalUser'] ?? null)) {
85  $userInformation .= ' via ' . $logData['originalUser'];
86  }
87 
88  $result = [
89  $row['uid'],
90  BackendUtility::datetime($row['tstamp']),
91  $userInformation,
92  $text,
93  ];
94 
95  if ($showDetails) {
96  $result[] = $this->‪arrayToLogString($row, [
97  'uid',
98  'userid',
99  'action',
100  'recuid',
101  'tablename',
102  'recpid',
103  'error',
104  'tstamp',
105  'type',
106  'details_nr',
107  'IP',
108  'event_pid',
109  'NEWid',
110  'workspace',
111  ]);
112  }
113  $content[] = $result;
114  }
115  $io->table($tableHeaders, $content);
116  return Command::SUCCESS;
117  }
118 
127  protected function ‪arrayToLogString(array $arr, array $valueList): string
128  {
129  $str = '';
130  foreach ($arr as $key => $value) {
131  if (in_array($key, $valueList, true)) {
132  $str .= (string)$key . trim(': ' . ‪GeneralUtility::fixed_lgd_cs(str_replace(LF, '|', (string)$value), 20)) . LF;
133  }
134  }
135  return $str;
136  }
137 }
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ListSysLogCommand.php:46
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixed_lgd_cs
‪static string fixed_lgd_cs(string $string, int $chars, string $appendString='...')
Definition: GeneralUtility.php:92
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetails
‪formatLogDetails(string $detailString, mixed $substitutes)
Definition: LogDataTrait.php:43
‪TYPO3\CMS\Core\Log\LogDataTrait\unserializeLogData
‪unserializeLogData(mixed $logData)
Definition: LogDataTrait.php:30
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\configure
‪configure()
Definition: ListSysLogCommand.php:38
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand
Definition: ListSysLogCommand.php:33
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Lowlevel\Command
Definition: CleanFlexFormsCommand.php:18
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\arrayToLogString
‪string arrayToLogString(array $arr, array $valueList)
Definition: ListSysLogCommand.php:126
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Log\LogDataTrait
Definition: LogDataTrait.php:25