59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
namespace ProVM\Logview\Parser;
|
|
|
|
use Error;
|
|
use Exception;
|
|
use Safe\DateTimeImmutable;
|
|
use function Safe\{fopen, fclose, preg_match_all, preg_match, error_log, json_encode};
|
|
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 = fopen($filename, 'r');
|
|
$sum = 0;
|
|
while(!feof($fh)) {
|
|
$line = fgets($fh);
|
|
$sum += preg_match_all($regex, $line);
|
|
}
|
|
fclose($fh);
|
|
return $sum;
|
|
} catch (Exception $e) {
|
|
return 0;
|
|
}
|
|
}
|
|
public function parse(mixed &$file_handler): Log
|
|
{
|
|
$log = parent::parse($file_handler);
|
|
$content = $log->getOriginal();
|
|
$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 {
|
|
preg_match($regex, $content, $matches);
|
|
} catch (Error $e) {
|
|
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(json_encode(['level' => $matches['level']], JSON_UNESCAPED_SLASHES));
|
|
if (count($extra) > 0) {
|
|
$log->setExtra(json_encode($extra, JSON_UNESCAPED_SLASHES));
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
}
|