Trace log prewiew counter + his API

This commit is contained in:
Václav Španinger 2020-07-30 16:31:25 +02:00
parent b174022f9a
commit a6cb51f2f5
6 changed files with 94 additions and 1 deletions

View File

@ -29,6 +29,7 @@ $router->get('/api/rooms/{roomId}/update', 'RoomsApi@update');
$router->get('/api/devices', 'DevicesApi@default'); $router->get('/api/devices', 'DevicesApi@default');
$router->get('/api/users', 'UsersApi@default'); $router->get('/api/users', 'UsersApi@default');
$router->get('/api/server', 'ServerApi@default'); $router->get('/api/server', 'ServerApi@default');
$router->get('/api/server/log', 'ServerApi@logStatus');
$router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run'); $router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run');
$router->post('/api/widgets/{widgetId}/detail', 'WidgetApi@detail'); $router->post('/api/widgets/{widgetId}/detail', 'WidgetApi@detail');

View File

@ -25,4 +25,11 @@ class ServerApi extends ApiController {
]; ];
$this->response($response); $this->response($response);
} }
public function logStatus()
{
$logKeeper = new LogMaintainer();
$response = $logKeeper::getStats();
$this->response($response);
}
} }

View File

@ -23,4 +23,56 @@ class LogMaintainer
$seconds = $days * 86400; $seconds = $days * 86400;
$this->cleaningDir ('../logs/', $seconds); $this->cleaningDir ('../logs/', $seconds);
} }
public static function getStats(){
$stats = array(
'ERROR' => 0,
'WARNING' => 0,
'EXEPTION' => 0,
'INFO' => 0,
);
$result = array();
$result = self::logFinder ('../logs/', $result);
foreach ($result as $path => $files) {
foreach ($files as $file) {
# code...
$matches = array();
$re = '/\[(?:warning|error|info)\]/';
$str = file_get_contents($path . $file);
preg_match_all($re, $str, $matches);
if (count($matches[0]) == 0) continue;
foreach ($matches[0] as $match) {
switch($match){
case '[error]': $stats['ERROR']++; break;
case '[warning]': $stats['WARNING']++; break;
case '[exeption]': $stats['EXEPTION']++; break;
default: $stats['INFO']++; break;
}
}
}
}
return $stats;
}
private static function logFinder ($dir, $result) {
$logFiles = scandir ($dir);
foreach ($logFiles as $key => $file) {
if (in_array ($file,array (".", "..", ".gitkeep")))
{
continue;
}
if (!is_dir($dir . $file)) {
$result[$dir][] = $file;
} else {
$result = self::logFinder ($dir . $file . "/", $result);
}
}
return $result;
}
} }

View File

@ -8,8 +8,12 @@ class LogRecordTypes{
'level' => 1, 'level' => 1,
'identifier' =>'warning', 'identifier' =>'warning',
]; ];
const INFO = [ const EXEPTION = [
'level' => 2, 'level' => 2,
'identifier' => 'exeption',
];
const INFO = [
'level' => 3,
'identifier' => 'info', 'identifier' => 'info',
]; ];
} }

View File

@ -22,6 +22,7 @@ class Log extends Template
$template->prepare('debugMod', DEBUGMOD); $template->prepare('debugMod', DEBUGMOD);
$template->prepare('logToLiveTime', LOGTIMOUT); $template->prepare('logToLiveTime', LOGTIMOUT);
$template->prepare('title', 'Logy'); $template->prepare('title', 'Logy');
$template->prepare('logStats', LogMaintainer::getStats());
$template->prepare('logsFiles', $result); $template->prepare('logsFiles', $result);
$template->prepare('langMng', $langMng); $template->prepare('langMng', $langMng);

View File

@ -23,6 +23,34 @@
<div class="col-md-9 main-body"> <div class="col-md-9 main-body">
<div class="col-12 col-sm-9 mx-auto mt-4"> <div class="col-12 col-sm-9 mx-auto mt-4">
<h1><?php echo $LANGMNG->get('t_Logs'); ?></h1> <h1><?php echo $LANGMNG->get('t_Logs'); ?></h1>
<div class="row mb-4">
<div class="col-6 col-lg-3">
<div class="panel alert alert-danger p-4 is-danger">
<h5><?php $LANGMNG->echo('error'); ?></h5>
<?php echo $LOGSTATS['ERROR'];?>
</div>
</div>
<div class="col-6 col-lg-3">
<div class="panel alert alert-warning p-4">
<h5><?php $LANGMNG->echo('warning'); ?></h5>
<?php echo $LOGSTATS['WARNING'];?>
</div>
</div>
<div class="col-6 col-lg-3">
<div class="panel alert alert-warning p-4">
<h5><?php $LANGMNG->echo('exeption'); ?></h5>
<?php echo $LOGSTATS['EXEPTION'];?>
</div>
</div>
<div class="col-6 col-lg-3">
<div class="panel alert p-4">
<h5><?php $LANGMNG->echo('log'); ?></h5>
<?php echo $LOGSTATS['INFO'];?>
</div>
</div>
</div>
<label><?php echo $LANGMNG->get('l_logMaxLiveTime') . " " . $LOGTOLIVETIME . " days";?></label></br> <label><?php echo $LANGMNG->get('l_logMaxLiveTime') . " " . $LOGTOLIVETIME . " days";?></label></br>
<form method="post" action=""> <form method="post" action="">