Add search and filter to log view

This commit is contained in:
fabrixxm 2021-05-24 21:47:10 +02:00
parent b8fc6a8c02
commit 5b9aeeeca9
5 changed files with 248 additions and 81 deletions

View file

@ -27,22 +27,38 @@ class ParsedLog
{
const REGEXP = '/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[^ ]*) (\w+) \[(\w*)\]: (.*)/';
/** @var int */
public $id = 0;
/** @var string */
public $date = null;
/** @var string */
public $context = null;
/** @var string */
public $level = null;
/** @var string */
public $message = null;
/** @var string */
public $data = null;
/** @var string */
public $source = null;
/** @var string */
public $logline;
/**
* @param int line id
* @param string $logline Source log line to parse
*/
public function __construct(int $id, string $logline)
{
$this->id = $id;
$this->parse($logline);
$this->stop = false;
}
private function parse($logline)
@ -60,30 +76,34 @@ class ParsedLog
$this->message = $matches[4];
$this->data = $jsondata;
$this->source = $jsonsource;
$this->try_fix_json('data');
$this->try_fix_json();
$this->logline = $logline;
}
/**
* Fix message / data split
*
* In log boundary between message and json data is not specified.
* If message contains '{' the parser thinks there starts the json data.
* This method try to parse the found json and if it fails, search for next '{'
* in json data and retry
*/
private function try_fix_json(string $key)
private function try_fix_json()
{
if (is_null($this->$key) || $this->$key == "") {
if (is_null($this->data) || $this->data == "") {
return;
}
try {
$d = json_decode($this->$key, true, 512, JSON_THROW_ON_ERROR);
$d = json_decode($this->data, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
// try to find next { in $str and move string before to 'message'
$pos = strpos($this->$key, '{', 1);
$pos = strpos($this->data, '{', 1);
$this->message .= substr($this->$key, 0, $pos);
$this->$key = substr($this->key, $pos);
$this->try_fix_json($key);
$this->message .= substr($this->data, 0, $pos);
$this->data = substr($this->data, $pos);
$this->try_fix_json();
}
}