Colors moved to template files, and namespace cleanups

This commit is contained in:
2023-05-18 16:59:14 -04:00
parent 0580cb5d30
commit 60a7ebb231
7 changed files with 99 additions and 73 deletions

View File

@ -3,8 +3,10 @@ namespace ProVM\Logview;
use DateTimeInterface;
use DateTimeImmutable;
use JsonSerializable;
use ProVM\Common\Define\Log as Definition;
class Log implements \ProVM\Common\Define\Log, \JsonSerializable
class Log implements Definition, JsonSerializable
{
public function __construct(?string $original = null)
{
@ -110,15 +112,6 @@ class Log implements \ProVM\Common\Define\Log, \JsonSerializable
return isset($this->context) and $this->context !== '';
}
public function getColor(): string
{
return self::COLORS[strtoupper($this->getSeverity())];
}
public function getBackgroundColor(): string
{
return self::BACKGROUNDS[strtoupper($this->getSeverity())];
}
public function jsonSerialize(): mixed
{
return ($this->parsed()) ? [
@ -147,26 +140,4 @@ class Log implements \ProVM\Common\Define\Log, \JsonSerializable
'EMERGENCY',
'DEPRECATED',
];
const COLORS = [
'DEBUG' => '#000',
'INFO' => '#fff',
'NOTICE' => '#fff',
'WARNING' => '#000',
'ERROR' => '#fff',
'CRITICAL' => '#fff',
'ALERT' => '#fff',
'EMERGENCY' => '#fff',
'DEPRECATED' => '#fff',
];
const BACKGROUNDS = [
'DEBUG' => '#fff',
'INFO' => '#00f',
'NOTICE' => '#55f',
'WARNING' => '#dd5',
'ERROR' => '#555',
'CRITICAL' => '#f00',
'ALERT' => '#f55',
'EMERGENCY' => '#f55',
'DEPRECATED' => '#f50',
];
}

View File

@ -1,7 +1,10 @@
<?php
namespace ProVM\Logview\Parser;
use Exception;
use Error;
use Safe\DateTimeImmutable;
use function Safe\{fopen, error_log, json_encode, preg_match, preg_match_all};
use ProVM\Common\Define\Log;
use ProVM\Common\Implement\Parser;
@ -11,16 +14,16 @@ class Monolog extends Parser
{
try {
$regex = "/\[(?P<date>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}(?:\+|-)\d{2}:\d{2})\]/";
$fh = \Safe\fopen($filename, 'r');
$fh = fopen($filename, 'r');
$sum = 0;
while(!feof($fh)) {
$line = fgets($fh);
$sum += \Safe\preg_match_all($regex, $line);
$sum += preg_match_all($regex, $line);
}
fclose($fh);
return $sum;
} catch (\Exception $e) {
\Safe\error_log($e . PHP_EOL, 3, '/logs/total.log');
} catch (Exception $e) {
error_log($e . PHP_EOL, 3, '/logs/total.log');
return 0;
}
}
@ -38,10 +41,10 @@ class Monolog extends Parser
];
$regex = implode('', $regex);
try {
\Safe\preg_match("/{$regex}/", $content, $matches);
} catch (\Exception $e) {
\Safe\error_log($content . PHP_EOL, 3, '/logs/debug.log');
\Safe\error_log($e . PHP_EOL, 3, '/logs/debug.log');
preg_match("/{$regex}/", $content, $matches);
} catch (Exception $e) {
error_log($content . PHP_EOL, 3, '/logs/debug.log');
error_log($e . PHP_EOL, 3, '/logs/debug.log');
return $log;
}
@ -49,7 +52,7 @@ class Monolog extends Parser
$extra = [];
try {
$log->setDate(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', $matches['date']));
} catch (\Exception $e) {
} catch (Exception $e) {
$log->setDate(new DateTimeImmutable());
$extra['date'] = $matches['date'];
}
@ -72,11 +75,11 @@ class Monolog extends Parser
$extra['extra'] = "{{$matches['extra']}}";
}
if (count($extra) > 0) {
$log->setExtra(\Safe\json_encode($extra, JSON_UNESCAPED_SLASHES));
$log->setExtra(json_encode($extra, JSON_UNESCAPED_SLASHES));
}
} catch (\Error $e) {
\Safe\error_log($e . PHP_EOL, 3, '/logs/debug.log');
\Safe\error_log(var_export($matches, true) . PHP_EOL, 3, '/logs/debug.log');
} catch (Error $e) {
error_log($e . PHP_EOL, 3, '/logs/debug.log');
error_log(var_export($matches, true) . PHP_EOL, 3, '/logs/debug.log');
}
return $log;

View File

@ -1,7 +1,10 @@
<?php
namespace ProVM\Logview\Parser;
use Error;
use Exception;
use Safe\DateTimeImmutable;
use function Safe\{fopen, preg_match_all, preg_match, error_log, json_encode};
use ProVM\Common\Define\Log;
use ProVM\Common\Implement\Parser;
@ -11,15 +14,15 @@ class PHPDefault extends Parser
{
try {
$regex = "/\[(?<date>\d{2}-\w{3}-\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3})\]/";
$fh = \Safe\fopen($filename, 'r');
$fh = fopen($filename, 'r');
$sum = 0;
while(!feof($fh)) {
$line = fgets($fh);
$sum += \Safe\preg_match_all($regex, $line);
$sum += preg_match_all($regex, $line);
}
fclose($fh);
return $sum;
} catch (\Exception $e) {
} catch (Exception $e) {
return 0;
}
}
@ -28,25 +31,25 @@ class PHPDefault extends Parser
$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');
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) {
} 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));
$log->setContext(json_encode(['level' => $matches['level']], JSON_UNESCAPED_SLASHES));
if (count($extra) > 0) {
$log->setExtra(\Safe\json_encode($extra, JSON_UNESCAPED_SLASHES));
$log->setExtra(json_encode($extra, JSON_UNESCAPED_SLASHES));
}
return $log;