Added multiline parsing
This commit is contained in:
@ -1,7 +1,30 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
interface Log
|
||||
{
|
||||
public function getOriginal(): string;
|
||||
public function getDate(): DateTimeInterface;
|
||||
public function getChannel(): string;
|
||||
public function getSeverity(): string;
|
||||
public function getMessage(): string;
|
||||
public function getStack(): array;
|
||||
public function getContext(): string;
|
||||
public function getExtra(): string;
|
||||
|
||||
public function setOriginal(string $original): Log;
|
||||
public function setDate(DateTimeInterface $dateTime): Log;
|
||||
public function setChannel(string $channel): Log;
|
||||
public function setSeverity(string $severity): Log;
|
||||
public function setMessage(string $message): Log;
|
||||
public function setStack(array $stack): Log;
|
||||
public function setContext(string $context): Log;
|
||||
public function setExtra(string $extra): Log;
|
||||
|
||||
public function parsed(): bool;
|
||||
|
||||
public function hasStack(): bool;
|
||||
public function hasContext(): bool;
|
||||
}
|
||||
|
@ -3,5 +3,32 @@ namespace ProVM\Common\Define;
|
||||
|
||||
interface Parser
|
||||
{
|
||||
public function parse(string $content): Log;
|
||||
/**
|
||||
* Determine if file is multiline
|
||||
* @param string $filename
|
||||
* @return bool
|
||||
*/
|
||||
public function isMultiline(string $filename): bool;
|
||||
|
||||
/**
|
||||
* Get the total amount of errors
|
||||
* @param string $filename
|
||||
* @return int
|
||||
*/
|
||||
public function total(string $filename): int;
|
||||
|
||||
/**
|
||||
* Parse line(s)
|
||||
* @param mixed &$file_handler
|
||||
* @return Log
|
||||
*/
|
||||
public function parse(mixed &$file_handler): Log;
|
||||
|
||||
/**
|
||||
* Advance $offset errors
|
||||
* @param mixed $file_handler
|
||||
* @param int $offset
|
||||
* @return void
|
||||
*/
|
||||
public function advance(mixed &$file_handler, int $offset): void;
|
||||
}
|
||||
|
@ -3,12 +3,19 @@ namespace ProVM\Common\Implement;
|
||||
|
||||
use Exception;
|
||||
use ProVM\Common\Define\Log;
|
||||
use ProVM\Logview\Exception\Parse\EmptyException;
|
||||
use ProVM\Logview\Exception\Parse\EmptyLineException;
|
||||
use ProVM\Logview\Log as LogContent;
|
||||
use ProVM\Common\Define\Parser as Definition;
|
||||
use function Safe\fopen;
|
||||
use function Safe\{fopen, fclose};
|
||||
|
||||
abstract class Parser implements Definition
|
||||
{
|
||||
public function isMultiline(string $filename): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function total(string $filename): int
|
||||
{
|
||||
try {
|
||||
@ -24,8 +31,31 @@ abstract class Parser implements Definition
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public function parse(string $content): Log
|
||||
public function parse(mixed &$file_handler): Log
|
||||
{
|
||||
$content = fgets($file_handler);
|
||||
if (!$content) {
|
||||
$meta_data = stream_get_meta_data($file_handler);
|
||||
throw new EmptyException($meta_data['uri'], ftell($file_handler));
|
||||
}
|
||||
if (trim($content) === '') {
|
||||
$meta_data = stream_get_meta_data($file_handler);
|
||||
throw new EmptyLineException($meta_data['uri'], ftell($file_handler));
|
||||
}
|
||||
return new LogContent($content);
|
||||
}
|
||||
public function advance(mixed &$file_handler, int $offset): void
|
||||
{
|
||||
if ($offset === 0) {
|
||||
return;
|
||||
}
|
||||
$cnt = 0;
|
||||
while(!feof($file_handler)) {
|
||||
fgets($file_handler);
|
||||
$cnt ++;
|
||||
if ($cnt >= $offset) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,8 +60,9 @@ class Logs
|
||||
$map = [
|
||||
Parsers\Access::class => '/(access.log)/',
|
||||
Parsers\Error::class => '/(error.log)/',
|
||||
Parsers\Debug::class => '/(debug.log)/',
|
||||
Parsers\Monolog::class => '/(-\d{4}-\d{2}-\d{2}.log)/',
|
||||
Parsers\PHPDefault::class => '/(php_errors.log)/'
|
||||
Parsers\PHPDefault::class => '/(php_errors.log)/',
|
||||
];
|
||||
foreach ($map as $class => $regex) {
|
||||
if (preg_match($regex, $filename) === 1) {
|
||||
|
Reference in New Issue
Block a user