diff --git a/lib/Command/Tail.php b/lib/Command/Tail.php index 0f317e72..ab125a0e 100644 --- a/lib/Command/Tail.php +++ b/lib/Command/Tail.php @@ -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; @@ -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; diff --git a/lib/Command/Watch.php b/lib/Command/Watch.php index 2b5048d3..b9e3817c 100644 --- a/lib/Command/Watch.php +++ b/lib/Command/Watch.php @@ -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; @@ -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(); } @@ -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 @@ -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;