App src code
This commit is contained in:
156
app/src/Log.php
Normal file
156
app/src/Log.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace ProVM\Logview;
|
||||
|
||||
use DateTimeInterface;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Log
|
||||
{
|
||||
protected DateTimeInterface $dateTime;
|
||||
protected string $channel;
|
||||
protected string $severity;
|
||||
protected string $message;
|
||||
protected array $stack;
|
||||
protected string $context;
|
||||
protected string $extra;
|
||||
|
||||
public function getDate(): DateTimeInterface
|
||||
{
|
||||
return $this->dateTime;
|
||||
}
|
||||
public function getChannel(): string
|
||||
{
|
||||
return $this->channel;
|
||||
}
|
||||
public function getSeverity(): string
|
||||
{
|
||||
return $this->severity;
|
||||
}
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
public function getStack(): array
|
||||
{
|
||||
return $this->stack ?? [];
|
||||
}
|
||||
public function getContext(): string
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
public function getExtra(): string
|
||||
{
|
||||
return $this->extra ?? '';
|
||||
}
|
||||
|
||||
public function setDate(DateTimeInterface $dateTime): Log
|
||||
{
|
||||
$this->dateTime = $dateTime;
|
||||
return $this;
|
||||
}
|
||||
public function setChannel(string $channel): Log
|
||||
{
|
||||
$this->channel = $channel;
|
||||
return $this;
|
||||
}
|
||||
public function setSeverity(string $severity): Log
|
||||
{
|
||||
$this->severity = $severity;
|
||||
return $this;
|
||||
}
|
||||
public function setMessage(string $message): Log
|
||||
{
|
||||
$this->message = $message;
|
||||
return $this;
|
||||
}
|
||||
public function setStack(array $stack): Log
|
||||
{
|
||||
$this->stack = $stack;
|
||||
return $this;
|
||||
}
|
||||
public function setContext(string $context): Log
|
||||
{
|
||||
$this->context = $context;
|
||||
return $this;
|
||||
}
|
||||
public function setExtra(string $extra): Log
|
||||
{
|
||||
$this->extra = $extra;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasStack(): bool
|
||||
{
|
||||
return isset($this->stack);
|
||||
}
|
||||
public function hasContext(): bool
|
||||
{
|
||||
return $this->context !== '';
|
||||
}
|
||||
|
||||
public function getColor(): string
|
||||
{
|
||||
return self::COLORS[strtoupper($this->getSeverity())];
|
||||
}
|
||||
public function getBackgroundColor(): string
|
||||
{
|
||||
return self::BACKGROUNDS[strtoupper($this->getSeverity())];
|
||||
}
|
||||
|
||||
public static function parse(string $content): Log
|
||||
{
|
||||
$log = new Log();
|
||||
|
||||
$regex = "/\[(?P<date>.*)\]\s(?<channel>\w*)\.(?<severity>\w*):\s(?<message>.*)\s[\[|\{](?<context>.*)[\]|\}]\s\[(?<extra>.*)\]/";
|
||||
preg_match($regex, $content, $matches);
|
||||
$log->setDate(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', $matches['date']));
|
||||
$log->setChannel($matches['channel']);
|
||||
$log->setSeverity($matches['severity']);
|
||||
$message = $matches['message'];
|
||||
if (str_contains($message, 'Stack trace')) {
|
||||
list($msg, $data) = explode('Stack trace:', $message);
|
||||
$message = trim($msg);
|
||||
$regex = '/\s#\d+\s/';
|
||||
$lines = preg_split($regex, $data);
|
||||
array_shift($lines);
|
||||
$log->setStack($lines);
|
||||
}
|
||||
$log->setMessage($message);
|
||||
$log->setContext($matches['context']);
|
||||
if (isset($matches['extra'])) {
|
||||
$log->setExtra($matches['extra']);
|
||||
}
|
||||
return $log;
|
||||
}
|
||||
|
||||
const LEVELS = [
|
||||
'DEBUG',
|
||||
'INFO',
|
||||
'NOTICE',
|
||||
'WARNING',
|
||||
'ERROR',
|
||||
'CRITICAL',
|
||||
'ALERT',
|
||||
'EMERGENCY',
|
||||
];
|
||||
const COLORS = [
|
||||
'DEBUG' => '#000',
|
||||
'INFO' => '#000',
|
||||
'NOTICE' => '#fff',
|
||||
'WARNING' => '#000',
|
||||
'ERROR' => '#fff',
|
||||
'CRITICAL' => '#fff',
|
||||
'ALERT' => '#fff',
|
||||
'EMERGENCY' => '#fff',
|
||||
];
|
||||
const BACKGROUNDS = [
|
||||
'DEBUG' => '#fff',
|
||||
'INFO' => '#00f',
|
||||
'NOTICE' => '#55f',
|
||||
'WARNING' => '#dd5',
|
||||
'ERROR' => '#555',
|
||||
'CRITICAL' => '#f00',
|
||||
'ALERT' => '#f55',
|
||||
'EMERGENCY' => '#f55',
|
||||
];
|
||||
}
|
44
app/src/Log/File.php
Normal file
44
app/src/Log/File.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace ProVM\Logview\Log;
|
||||
|
||||
use Generator;
|
||||
use ProVM\Logview\Log;
|
||||
|
||||
class File
|
||||
{
|
||||
protected string $filename;
|
||||
protected string $content;
|
||||
|
||||
public function getFilename(): string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function setFilename(string $filename): File
|
||||
{
|
||||
$this->filename = $filename;
|
||||
return $this;
|
||||
}
|
||||
public function setContent(string $content): File
|
||||
{
|
||||
$this->content = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLogs(): array
|
||||
{
|
||||
$lines = explode(PHP_EOL, $this->getContent());
|
||||
$logs = [];
|
||||
foreach ($lines as $line) {
|
||||
if (trim($line) === '') {
|
||||
continue;
|
||||
}
|
||||
$logs []= Log::parse($line);
|
||||
}
|
||||
return array_reverse($logs);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user