Skip to content

Commit 0718de6

Browse files
committed
add option for raw output in log:watch/log:tail
Signed-off-by: Robin Appelman <[email protected]>
1 parent b0d11b2 commit 0718de6

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

lib/Command/Tail.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Symfony\Component\Console\Input\InputArgument;
3030
use Symfony\Component\Console\Input\InputInterface;
3131
use Symfony\Component\Console\Input\InputOption;
32-
use Symfony\Component\Console\Input\StringInput;
3332
use Symfony\Component\Console\Output\OutputInterface;
3433
use Symfony\Component\Console\Style\SymfonyStyle;
3534
use Symfony\Component\Console\Terminal;
@@ -51,42 +50,49 @@ protected function configure() {
5150
->setName('log:tail')
5251
->setDescription('Tail the nextcloud logfile')
5352
->addArgument('lines', InputArgument::OPTIONAL, 'The number of log entries to print', "10")
54-
->addOption('follow', 'f', InputOption::VALUE_NONE, 'Output new log entries as they appear');
53+
->addOption('follow', 'f', InputOption::VALUE_NONE, 'Output new log entries as they appear')
54+
->addOption('raw', 'r', InputOption::VALUE_NONE, 'Output raw log json instead of formatted log item');
5555
parent::configure();
5656
}
5757

5858
protected function execute(InputInterface $input, OutputInterface $output): int {
59+
$raw = $input->getOption('raw');
5960
$count = (int)$input->getArgument('lines');
60-
$terminal = new Terminal();
61-
$totalWidth = $terminal->getWidth();
62-
// 8 level, 18 for app, 26 for time, 6 for formatting
63-
$messageWidth = $totalWidth - 8 - 18 - 26 - 6;
6461
$io = new SymfonyStyle($input, $output);
6562
$logIterator = $this->logIteratorFactory->getLogIterator('11111');
66-
$i = 0;
67-
$tableItems = [];
68-
foreach ($logIterator as $logItem) {
69-
$i++;
70-
if ($i > $count) {
71-
break;
63+
$logIterator = new \LimitIterator($logIterator, 0, $count);
64+
$logItems = iterator_to_array($logIterator);
65+
$logItems = array_reverse($logItems);
66+
67+
if ($raw) {
68+
foreach ($logItems as $logItem) {
69+
$output->writeln(json_encode($logItem));
7270
}
73-
$tableItems[] = [
74-
self::LEVELS[$logItem['level']],
75-
wordwrap($logItem['app'], 18),
76-
$this->formatter->formatMessage($logItem, $messageWidth) . "\n",
77-
$logItem['time']
78-
];
71+
} else {
72+
$terminal = new Terminal();
73+
$totalWidth = $terminal->getWidth();
74+
// 8 level, 18 for app, 26 for time, 6 for formatting
75+
$messageWidth = $totalWidth - 8 - 18 - 26 - 6;
76+
77+
$tableItems = array_map(function (array $logItem) use ($messageWidth) {
78+
return [
79+
self::LEVELS[$logItem['level']],
80+
wordwrap($logItem['app'], 18),
81+
$this->formatter->formatMessage($logItem, $messageWidth) . "\n",
82+
$logItem['time'],
83+
];
84+
}, $logItems);
85+
$io->table([
86+
'Level',
87+
'App',
88+
'Message',
89+
'Time',
90+
], $tableItems);
7991
}
80-
$io->table([
81-
'Level',
82-
'App',
83-
'Message',
84-
'Time'
85-
], array_reverse($tableItems));
8692

8793
if ($input->getOption('follow')) {
8894
$watch = new Watch($this->formatter, $this->logIteratorFactory);
89-
$watch->run(new StringInput(''), $output);
95+
$watch->watch($raw, $output);
9096
}
9197

9298
return 0;

lib/Command/Watch.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCA\LogReader\Log\Formatter;
2929
use OCA\LogReader\Log\LogIteratorFactory;
3030
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
3132
use Symfony\Component\Console\Output\OutputInterface;
3233
use Symfony\Component\Console\Terminal;
3334

@@ -47,7 +48,8 @@ public function __construct(Formatter $formatter, LogIteratorFactory $logIterato
4748
protected function configure() {
4849
$this
4950
->setName('log:watch')
50-
->setDescription('Watch the nextcloud logfile');
51+
->setDescription('Watch the nextcloud logfile')
52+
->addOption('raw', 'r', InputOption::VALUE_NONE, 'Output raw log json instead of formatted log item');
5153
parent::configure();
5254
}
5355

@@ -60,6 +62,11 @@ private function getLastLogId() {
6062
}
6163

6264
protected function execute(InputInterface $input, OutputInterface $output): int {
65+
$raw = $input->getOption('raw');
66+
return $this->watch($raw, $output);
67+
}
68+
69+
public function watch(bool $raw, OutputInterface $output): int {
6370
$terminal = new Terminal();
6471
$totalWidth = $terminal->getWidth();
6572
// 8 level, 18 for app, 26 for time, 6 for formatting
@@ -97,8 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
97104
array_reverse($lines);
98105

99106
foreach ($lines as $line) {
100-
$this->printItem($line, $output, $messageWidth);
101-
$output->writeln("");
107+
if ($raw) {
108+
$output->writeln(json_encode($line));
109+
} else {
110+
$this->printItem($line, $output, $messageWidth);
111+
$output->writeln("");
112+
}
102113
}
103114

104115
$lastId = $id;

0 commit comments

Comments
 (0)