Display structured logs in admin

Tries to parse log lines and to display info in a table.
Additional JSON data is parsed and displayed clicking on a row.

File reading and line parsing is handled in iterators, to avoid to keep
too much data in memory.
Search and filter should be trivial to add.
Log file is read backward to display log events newest first.
A "tail" functionality should be easy to implement.
This commit is contained in:
fabrixxm 2021-03-27 18:28:09 +01:00
parent 4b3f1cbdb9
commit 9368f5445d
6 changed files with 361 additions and 25 deletions

View file

@ -21,49 +21,42 @@
namespace Friendica\Module\Admin\Logs;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Core\Renderer;
use Friendica\Core\Theme;
use Friendica\Module\BaseAdmin;
use Friendica\Util\Strings;
use Friendica\Model\Log\ParsedLogIterator;
class View extends BaseAdmin
{
const LIMIT = 500;
public static function content(array $parameters = [])
{
parent::content($parameters);
$t = Renderer::getMarkupTemplate('admin/logs/view.tpl');
DI::page()->registerFooterScript(Theme::getPathForFile('js/module/admin/logs/view.js'));
$f = DI::config()->get('system', 'logfile');
$data = '';
$data = null;
$error = null;
if (!file_exists($f)) {
$data = DI::l10n()->t('Error trying to open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s exist and is readable.', $f);
$error = DI::l10n()->t('Error trying to open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s exist and is readable.', $f);
} else {
$fp = fopen($f, 'r');
if (!$fp) {
$data = DI::l10n()->t('Couldn\'t open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s is readable.', $f);
} else {
$fstat = fstat($fp);
$size = $fstat['size'];
if ($size != 0) {
if ($size > 5000000 || $size < 0) {
$size = 5000000;
}
$seek = fseek($fp, 0 - $size, SEEK_END);
if ($seek === 0) {
$data = Strings::escapeHtml(fread($fp, $size));
while (!feof($fp)) {
$data .= Strings::escapeHtml(fread($fp, 4096));
}
}
}
fclose($fp);
try {
$data = new ParsedLogIterator($f, self::LIMIT);
} catch (Exception $e) {
$error = DI::l10n()->t('Couldn\'t open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s is readable.', $f);
}
}
return Renderer::replaceMacros($t, [
'$title' => DI::l10n()->t('Administration'),
'$page' => DI::l10n()->t('View Logs'),
'$data' => $data,
'$error' => $error,
'$logname' => DI::config()->get('system', 'logfile')
]);
}