Reverse order and multiple log files handler

This commit is contained in:
2023-02-15 17:45:23 -03:00
parent 110f37e4f4
commit c29eece81c
16 changed files with 494 additions and 101 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace ProVM\Logview\Parser;
use Safe\DateTimeImmutable;
use ProVM\Common\Define\Log;
use ProVM\Common\Implement\Parser;
class PHPDefault extends Parser
{
public function total(string $filename): int
{
try {
$regex = "/\[(?<date>\d{2}-\w{3}-\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3})\]/";
$fh = \Safe\fopen($filename, 'r');
$sum = 0;
while(!feof($fh)) {
$line = fgets($fh);
$sum += \Safe\preg_match_all($regex, $line);
}
fclose($fh);
return $sum;
} catch (\Exception $e) {
return 0;
}
}
public function parse(string $content): Log
{
$log = parent::parse($content);
$regex = "/\[(?<date>\d{2}-\w{3}-\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3})\]\s(?<level>PHP|User)\s(?<severity>\w+):\s(?<message>.*)/";
try {
\Safe\preg_match($regex, $content, $matches);
} catch (\Error $e) {
\Safe\error_log($e . PHP_EOL, 3, '/logs/debug.log');
return $log;
}
$extra = [];
try {
$log->setDate(DateTimeImmutable::createFromFormat('d-M-Y H:i:s e', $matches['date']));
} catch (\Exception $e) {
$log->setDate(new DateTimeImmutable());
$extra['date'] = $matches['date'];
}
$log->setChannel('');
$log->setSeverity($matches['severity']);
$log->setMessage($matches['message']);
$log->setContext(\Safe\json_encode(['level' => $matches['level']], JSON_UNESCAPED_SLASHES));
if (count($extra) > 0) {
$log->setExtra(\Safe\json_encode($extra, JSON_UNESCAPED_SLASHES));
}
return $log;
}
}