setName('logviewer')
->addOption(
'file',
'f',
InputOption::VALUE_OPTIONAL,
'custom log file location (default = grav.log)'
)
->addOption(
'lines',
'l',
InputOption::VALUE_OPTIONAL,
'number of lines (default = 10)'
)
->setDescription('Display the last few entries of Grav log')
->setHelp('Display the last few entries of Grav log');
}
/**
* @return int
*/
protected function serve(): int
{
$input = $this->getInput();
$io = $this->getIO();
$file = $input->getOption('file') ?? 'grav.log';
$lines = $input->getOption('lines') ?? 20;
$verbose = $input->getOption('verbose') ?? false;
$io->title('Log Viewer');
$io->writeln(sprintf('viewing last %s entries in %s', $lines, $file));
$io->newLine();
$viewer = new LogViewer();
$grav = Grav::instance();
$logfile = $grav['locator']->findResource('log://' . $file);
if (!$logfile) {
$io->error('cannot find the log file: logs/' . $file);
return 1;
}
$rows = $viewer->objectTail($logfile, $lines, true);
foreach ($rows as $log) {
$date = $log['date'];
$level_color = LogViewer::levelColor($log['level']);
if ($date instanceof DateTime) {
$output = "{$log['date']->format('Y-m-d h:i:s')} [<{$level_color}>{$log['level']}{$level_color}>]";
if ($log['trace'] && $verbose) {
$output .= " {$log['message']}\n";
foreach ((array) $log['trace'] as $index => $tracerow) {
$output .= "{$index}{$tracerow}\n";
}
} else {
$output .= " {$log['message']}";
}
$io->writeln($output);
}
}
return 0;
}
}