‪TYPO3CMS  11.5
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 
51  protected function ‪execute(InputInterface $input, OutputInterface ‪$output)
52  {
53  $io = new SymfonyStyle($input, ‪$output);
54  $io->title($this->getDescription());
55  $showDetails = ‪$output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL;
56 
57  $tableHeaders = [
58  'Log ID',
59  'Date & Time',
60  'User ID',
61  'Message',
62  ];
63  if ($showDetails) {
64  $tableHeaders[] = 'Details';
65  }
66 
67  // Initialize result array
68  $content = [];
69 
70  // Select DB relations from reference table
71  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
72  $rowIterator = $queryBuilder
73  ->select('*')
74  ->from('sys_log')
75  ->where(
76  $queryBuilder->expr()->gt(
77  'tstamp',
78  $queryBuilder->createNamedParameter(‪$GLOBALS['EXEC_TIME'] - 24 * 3600, ‪Connection::PARAM_INT)
79  )
80  )
81  ->orderBy('tstamp', 'DESC')
82  ->executeQuery();
83 
84  while ($row = $rowIterator->fetchAssociative()) {
85  $logData = $this->‪unserializeLogData($row['log_data'] ?? '');
86  $text = $this->‪formatLogDetails($row['details'] ?? '', $logData);
87  $userInformation = $row['userid'];
88  if (!empty($logData['originalUser'] ?? null)) {
89  $userInformation .= ' via ' . $logData['originalUser'];
90  }
91 
92  $result = [
93  $row['uid'],
94  BackendUtility::datetime($row['tstamp']),
95  $userInformation,
96  $text,
97  ];
98 
99  if ($showDetails) {
100  $result[] = $this->‪arrayToLogString($row, [
101  'uid',
102  'userid',
103  'action',
104  'recuid',
105  'tablename',
106  'recpid',
107  'error',
108  'tstamp',
109  'type',
110  'details_nr',
111  'IP',
112  'event_pid',
113  'NEWid',
114  'workspace',
115  ]);
116  }
117  $content[] = $result;
118  }
119  $io->table($tableHeaders, $content);
120  return 0;
121  }
122 
131  protected function ‪arrayToLogString(array $arr, array $valueList): string
132  {
133  $str = '';
134  foreach ($arr as $key => $value) {
135  if (in_array($key, $valueList, true)) {
136  $str .= (string)$key . trim(': ' . GeneralUtility::fixed_lgd_cs(str_replace(LF, '|', (string)$value), 20)) . LF;
137  }
138  }
139  return $str;
140  }
141 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\execute
‪int execute(InputInterface $input, OutputInterface $output)
Definition: ListSysLogCommand.php:50
‪TYPO3\CMS\Core\Log\LogDataTrait\unserializeLogData
‪array null unserializeLogData($logData)
Definition: LogDataTrait.php:33
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\configure
‪configure()
Definition: ListSysLogCommand.php:38
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetails
‪string formatLogDetails(string $detailString, $substitutes)
Definition: LogDataTrait.php:50
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand
Definition: ListSysLogCommand.php:33
‪$output
‪$output
Definition: annotationChecker.php:121
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪$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:130
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Log\LogDataTrait
Definition: LogDataTrait.php:25