‪TYPO3CMS  9.5
ListSysLogCommand.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Symfony\Component\Console\Command\Command;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20 use Symfony\Component\Console\Style\SymfonyStyle;
24 
29 class ‪ListSysLogCommand extends Command
30 {
31 
35  public function ‪configure()
36  {
37  $this->setDescription('Show entries from the sys_log database table of the last 24 hours.');
38  $this->setHelp('Prints a list of recent sys_log entries.' . LF . 'If you want to get more detailed information, use the --verbose option.');
39  }
40 
47  protected function ‪execute(InputInterface $input, OutputInterface ‪$output)
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, \PDO::PARAM_INT)
75  )
76  )
77  ->orderBy('tstamp', 'DESC')
78  ->execute();
79 
80  while ($row = $rowIterator->fetch()) {
81  $logData = unserialize($row['log_data']);
82  $userInformation = $row['userid'];
83  if (!empty($logData['originalUser'])) {
84  $userInformation .= ' via ' . $logData['originalUser'];
85  }
86 
87  $result = [
88  $row['uid'],
89  ‪BackendUtility::datetime($row['tstamp']),
90  $userInformation,
91  sprintf($row['details'], $logData[0], $logData[1], $logData[2], $logData[3], $logData[4], $logData[5])
92  ];
93 
94  if ($showDetails) {
95  $result[] = $this->‪arrayToLogString($row, [
96  'uid',
97  'userid',
98  'action',
99  'recuid',
100  'tablename',
101  'recpid',
102  'error',
103  'tstamp',
104  'type',
105  'details_nr',
106  'IP',
107  'event_pid',
108  'NEWid',
109  'workspace'
110  ]);
111  }
112  $content[] = $result;
113  }
114  $io->table($tableHeaders, $content);
115  }
116 
126  protected function ‪arrayToLogString(array $arr, array $valueList): string
127  {
128  $str = '';
129  foreach ($arr as $key => $value) {
130  if (in_array($key, $valueList, true)) {
131  $str .= (string)$key . trim(': ' . GeneralUtility::fixed_lgd_cs(str_replace(LF, '|', (string)$value), 20)) . LF;
132  }
133  }
134  return $str;
135  }
136 }
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ListSysLogCommand.php:47
‪TYPO3\CMS\Backend\Utility\BackendUtility\datetime
‪static string datetime($value)
Definition: BackendUtility.php:1190
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\configure
‪configure()
Definition: ListSysLogCommand.php:35
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand
Definition: ListSysLogCommand.php:30
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪$output
‪$output
Definition: annotationChecker.php:113
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Lowlevel\Command
Definition: CleanFlexFormsCommand.php:3
‪TYPO3\CMS\Lowlevel\Command\ListSysLogCommand\arrayToLogString
‪string arrayToLogString(array $arr, array $valueList)
Definition: ListSysLogCommand.php:126
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45