Skip to content

Commit

Permalink
add option for raw output in log:watch/log:tail
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Jun 16, 2023
1 parent b0d11b2 commit 9e58650
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
56 changes: 31 additions & 25 deletions lib/Command/Tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
Expand All @@ -51,42 +50,49 @@ protected function configure() {
->setName('log:tail')
->setDescription('Tail the nextcloud logfile')
->addArgument('lines', InputArgument::OPTIONAL, 'The number of log entries to print', "10")
->addOption('follow', 'f', InputOption::VALUE_NONE, 'Output new log entries as they appear');
->addOption('follow', 'f', InputOption::VALUE_NONE, 'Output new log entries as they appear')
->addOption('raw', 'r', InputOption::VALUE_NONE, 'Output raw log json instead of formatted log item');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$raw = $input->getOption('raw');
$count = (int)$input->getArgument('lines');
$terminal = new Terminal();
$totalWidth = $terminal->getWidth();
// 8 level, 18 for app, 26 for time, 6 for formatting
$messageWidth = $totalWidth - 8 - 18 - 26 - 6;
$io = new SymfonyStyle($input, $output);
$logIterator = $this->logIteratorFactory->getLogIterator('11111');
$i = 0;
$tableItems = [];
foreach ($logIterator as $logItem) {
$i++;
if ($i > $count) {
break;
$logIterator = new \LimitIterator($logIterator, 0, $count);
$logItems = iterator_to_array($logIterator);
$logItems = array_reverse($logItems);

if ($raw) {
foreach($logItems as $logItem) {
$output->writeln(json_encode($logItem));
}
$tableItems[] = [
self::LEVELS[$logItem['level']],
wordwrap($logItem['app'], 18),
$this->formatter->formatMessage($logItem, $messageWidth) . "\n",
$logItem['time']
];
} else {
$terminal = new Terminal();
$totalWidth = $terminal->getWidth();
// 8 level, 18 for app, 26 for time, 6 for formatting
$messageWidth = $totalWidth - 8 - 18 - 26 - 6;

$tableItems = array_map(function(array $logItem) use ($messageWidth) {
return [
self::LEVELS[$logItem['level']],
wordwrap($logItem['app'], 18),
$this->formatter->formatMessage($logItem, $messageWidth) . "\n",
$logItem['time'],
];
}, $logItems);
$io->table([
'Level',
'App',
'Message',
'Time',
], $tableItems);
}
$io->table([
'Level',
'App',
'Message',
'Time'
], array_reverse($tableItems));

if ($input->getOption('follow')) {
$watch = new Watch($this->formatter, $this->logIteratorFactory);
$watch->run(new StringInput(''), $output);
$watch->watch($raw, $output);
}

return 0;
Expand Down
17 changes: 14 additions & 3 deletions lib/Command/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCA\LogReader\Log\Formatter;
use OCA\LogReader\Log\LogIteratorFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;

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

Expand All @@ -60,6 +62,11 @@ private function getLastLogId() {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$raw = $input->getOption('raw');
return $this->watch($raw, $output);
}

public function watch(bool $raw, OutputInterface $output): int {
$terminal = new Terminal();
$totalWidth = $terminal->getWidth();
// 8 level, 18 for app, 26 for time, 6 for formatting
Expand Down Expand Up @@ -97,8 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
array_reverse($lines);

foreach ($lines as $line) {
$this->printItem($line, $output, $messageWidth);
$output->writeln("");
if ($raw) {
$output->writeln(json_encode($line));
} else {
$this->printItem($line, $output, $messageWidth);
$output->writeln("");
}
}

$lastId = $id;
Expand Down

0 comments on commit 9e58650

Please sign in to comment.