Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for raw output in log:watch/log:tail #905

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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