Colors moved to template files, and namespace cleanups
This commit is contained in:
@ -5,7 +5,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim\Views\Blade as View;
|
||||
use ProVM\Common\Service\Logs as Service;
|
||||
use ProVM\Logview\Log;
|
||||
use function Safe\json_encode;
|
||||
|
||||
class Logs
|
||||
{
|
||||
@ -13,16 +13,9 @@ class Logs
|
||||
{
|
||||
$log = $service->get($log_file);
|
||||
|
||||
$levels = [];
|
||||
foreach (Log::LEVELS as $level) {
|
||||
$levels[strtolower($level)] = (object) [
|
||||
'text' => Log::COLORS[$level],
|
||||
'background' => Log::BACKGROUNDS[$level],
|
||||
];
|
||||
}
|
||||
return $view->render($response, 'logs.show', compact('log', 'levels'));
|
||||
return $view->render($response, 'logs.show', compact('log'));
|
||||
}
|
||||
public function getMore(ServerRequestInterface $request, ResponseInterface $response, View $view, Service $service, string $log_file, int $start = 0, int $amount = 100): ResponseInterface
|
||||
public function getMore(ServerRequestInterface $request, ResponseInterface $response, Service $service, string $log_file, int $start = 0, int $amount = 100): ResponseInterface
|
||||
{
|
||||
$log = $service->get($log_file);
|
||||
|
||||
@ -32,7 +25,7 @@ class Logs
|
||||
}
|
||||
$logs = array_reverse($logs);
|
||||
$total = $log->getTotal();
|
||||
$response->getBody()->write(\Safe\json_encode([
|
||||
$response->getBody()->write(json_encode([
|
||||
'total' => $total,
|
||||
'logs' => $logs
|
||||
]));
|
||||
|
@ -1,14 +1,18 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Implement;
|
||||
|
||||
use Exception;
|
||||
use ProVM\Common\Define\Log;
|
||||
use ProVM\Logview\Log as LogContent;
|
||||
use ProVM\Common\Define\Parser as Definition;
|
||||
use function Safe\fopen;
|
||||
|
||||
abstract class Parser implements \ProVM\Common\Define\Parser
|
||||
abstract class Parser implements Definition
|
||||
{
|
||||
public function total(string $filename): int
|
||||
{
|
||||
try {
|
||||
$fh = \Safe\fopen($filename, 'r');
|
||||
$fh = fopen($filename, 'r');
|
||||
$cnt = 0;
|
||||
while(!feof($fh)) {
|
||||
$line = fgets($fh);
|
||||
@ -16,12 +20,12 @@ abstract class Parser implements \ProVM\Common\Define\Parser
|
||||
}
|
||||
fclose($fh);
|
||||
return $cnt;
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public function parse(string $content): Log
|
||||
{
|
||||
return new \ProVM\Logview\Log($content);
|
||||
return new LogContent($content);
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,14 @@ namespace ProVM\Common\Service;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use SplFileInfo;
|
||||
use FilesystemIterator;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Logview\Log\File;
|
||||
use ProVM\Common\Define\Parser;
|
||||
use ProVM\Logview\Parser as Parsers;
|
||||
|
||||
use function Safe\{preg_match};
|
||||
|
||||
class Logs
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, string $folder)
|
||||
@ -42,7 +45,7 @@ class Logs
|
||||
|
||||
public function getFiles(): array
|
||||
{
|
||||
$files = new \FilesystemIterator($this->getFolder());
|
||||
$files = new FilesystemIterator($this->getFolder());
|
||||
$output = [];
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
@ -61,7 +64,7 @@ class Logs
|
||||
Parsers\PHPDefault::class => '/(php_errors.log)/'
|
||||
];
|
||||
foreach ($map as $class => $regex) {
|
||||
if (\Safe\preg_match($regex, $filename) === 1) {
|
||||
if (preg_match($regex, $filename) === 1) {
|
||||
return new $class;
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,52 @@
|
||||
@push('page_styles')
|
||||
<style>
|
||||
body {overflow-y:scroll;}
|
||||
@foreach ($levels as $level => $colors)
|
||||
{{--@foreach ($levels as $level => $colors)
|
||||
.{{$level}} {background-color: {{$colors->background}}; color: {{$colors->text}};}
|
||||
@endforeach
|
||||
@endforeach--}}
|
||||
</style>
|
||||
@endpush
|
||||
|
||||
@push('page_scripts')
|
||||
<script type="text/javascript">
|
||||
const colors = {
|
||||
debug: {
|
||||
color: '#000',
|
||||
background: '#fff'
|
||||
},
|
||||
info: {
|
||||
color: '#fff',
|
||||
background: '#2727e8'
|
||||
},
|
||||
notice: {
|
||||
color: '#fff',
|
||||
background: '#55f'
|
||||
},
|
||||
warning: {
|
||||
color: '#000',
|
||||
background: '#f5f580'
|
||||
},
|
||||
error: {
|
||||
color: '#fff',
|
||||
background: '#464646'
|
||||
},
|
||||
critical: {
|
||||
color: '#fff',
|
||||
background: '#cb0000'
|
||||
},
|
||||
alert: {
|
||||
color: '#fff',
|
||||
background: '#ec6060'
|
||||
},
|
||||
emergency: {
|
||||
color: '#fff',
|
||||
background: '#b95757'
|
||||
},
|
||||
deprecated: {
|
||||
color: '#fff',
|
||||
background: '#ce4000'
|
||||
}
|
||||
}
|
||||
const icons = {
|
||||
debug: 'bug',
|
||||
info: 'search',
|
||||
@ -159,7 +197,18 @@
|
||||
ob.observe(watch)
|
||||
}
|
||||
}
|
||||
|
||||
function buildColors() {
|
||||
const styleDOM = document.createElement('style')
|
||||
const styles = []
|
||||
Object.keys(colors).forEach(level => {
|
||||
styles.push('.' + level + ' {color: ' + colors[level].color + '; background-color: ' + colors[level].background + '}')
|
||||
})
|
||||
styleDOM.innerHTML = styles.join("\n")
|
||||
document.getElementsByTagName('head')[0].appendChild(styleDOM)
|
||||
}
|
||||
$(document).ready(() => {
|
||||
buildColors()
|
||||
logs.setup('logs')
|
||||
})
|
||||
</script>
|
||||
|
@ -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',
|
||||
];
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user