‪TYPO3CMS  10.4
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;
25 
30 class ‪ListSysLogCommand extends Command
31 {
32 
36  public function ‪configure()
37  {
38  $this->setDescription('Show entries from the sys_log database table of the last 24 hours.');
39  $this->setHelp('Prints a list of recent sys_log entries.' . LF . 'If you want to get more detailed information, use the --verbose option.');
40  }
41 
49  protected function ‪execute(InputInterface $input, OutputInterface ‪$output)
50  {
51  $io = new SymfonyStyle($input, ‪$output);
52  $io->title($this->getDescription());
53  $showDetails = ‪$output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL;
54 
55  $tableHeaders = [
56  'Log ID',
57  'Date & Time',
58  'User ID',
59  'Message'
60  ];
61  if ($showDetails) {
62  $tableHeaders[] = 'Details';
63  }
64 
65  // Initialize result array
66  $content = [];
67 
68  // Select DB relations from reference table
69  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
70  $rowIterator = $queryBuilder
71  ->select('*')
72  ->from('sys_log')
73  ->where(
74  $queryBuilder->expr()->gt(
75  'tstamp',
76  $queryBuilder->createNamedParameter(‪$GLOBALS['EXEC_TIME'] - 24 * 3600, \PDO::PARAM_INT)
77  )
78  )
79  ->orderBy('tstamp', 'DESC')
80  ->execute();
81 
82  while ($row = $rowIterator->fetch()) {
83  $logData = unserialize($row['log_data']);
84  $userInformation = $row['userid'];
85  if (!empty($logData['originalUser'])) {
86  $userInformation .= ' via ' . $logData['originalUser'];
87  }
88 
89  $result = [
90  $row['uid'],
91  ‪BackendUtility::datetime($row['tstamp']),
92  $userInformation,
93  sprintf($row['details'], $logData[0], $logData[1], $logData[2], $logData[3], $logData[4], $logData[5])
94  ];
95 
96  if ($showDetails) {
97  $result[] = $this->‪arrayToLogString($row, [
98  'uid',
99  'userid',
100  'action',
101  'recuid',
102  'tablename',
103  'recpid',
104  'error',
105  'tstamp',
106  'type',
107  'details_nr',
108  'IP',
109  'event_pid',
110  'NEWid',
111  'workspace'
112  ]);
113  }
114  $content[] = $result;
115  }
116  $io->table($tableHeaders, $content);
117  return 0;
118  }
119 
128  protected function ‪arrayToLogString(array $arr, array $valueList): string
129  {
130  $str = '';
131  foreach ($arr as $key => $value) {
132  if (in_array($key, $valueList, true)) {
133  $str .= (string)$key . trim(': ' . GeneralUtility::fixed_lgd_cs(str_replace(LF, '|', (string)$value), 20)) . LF;
134  }
135  }
136  return $str;
137  }
138 }
‪TYPO3\CMS\Backend\Utility\BackendUtility\datetime
‪static string datetime($value)
Definition: BackendUtility.php:989
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\execute
‪int execute(InputInterface $input, OutputInterface $output)
Definition: ListSysLogCommand.php:49
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\configure
‪configure()
Definition: ListSysLogCommand.php:36
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand
Definition: ListSysLogCommand.php:31
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪$output
‪$output
Definition: annotationChecker.php:119
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Lowlevel\Command
Definition: CleanFlexFormsCommand.php:18
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\arrayToLogString
‪string arrayToLogString(array $arr, array $valueList)
Definition: ListSysLogCommand.php:128
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46