From a6cb51f2f595db34be47463ed15e5fe822edfbb2 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Thu, 30 Jul 2020 16:31:25 +0200 Subject: [PATCH] Trace log prewiew counter + his API --- app/Routes.php | 1 + app/api/ServerApi.php | 7 ++++ app/models/managers/LogMaintainer.php | 52 +++++++++++++++++++++++++++ app/models/types/LogRecordTypes.php | 6 +++- app/views/Log.php | 1 + app/views/templates/log.phtml | 28 +++++++++++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/app/Routes.php b/app/Routes.php index 41513e5..3c87ccb 100644 --- a/app/Routes.php +++ b/app/Routes.php @@ -29,6 +29,7 @@ $router->get('/api/rooms/{roomId}/update', 'RoomsApi@update'); $router->get('/api/devices', 'DevicesApi@default'); $router->get('/api/users', 'UsersApi@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}/detail', 'WidgetApi@detail'); diff --git a/app/api/ServerApi.php b/app/api/ServerApi.php index 4685e98..4a4e3bc 100644 --- a/app/api/ServerApi.php +++ b/app/api/ServerApi.php @@ -25,4 +25,11 @@ class ServerApi extends ApiController { ]; $this->response($response); } + + public function logStatus() + { + $logKeeper = new LogMaintainer(); + $response = $logKeeper::getStats(); + $this->response($response); + } } diff --git a/app/models/managers/LogMaintainer.php b/app/models/managers/LogMaintainer.php index b852d7c..c596820 100644 --- a/app/models/managers/LogMaintainer.php +++ b/app/models/managers/LogMaintainer.php @@ -23,4 +23,56 @@ class LogMaintainer $seconds = $days * 86400; $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; + } } diff --git a/app/models/types/LogRecordTypes.php b/app/models/types/LogRecordTypes.php index 95e9222..183e14a 100644 --- a/app/models/types/LogRecordTypes.php +++ b/app/models/types/LogRecordTypes.php @@ -8,8 +8,12 @@ class LogRecordTypes{ 'level' => 1, 'identifier' =>'warning', ]; - const INFO = [ + const EXEPTION = [ 'level' => 2, + 'identifier' => 'exeption', + ]; + const INFO = [ + 'level' => 3, 'identifier' => 'info', ]; } \ No newline at end of file diff --git a/app/views/Log.php b/app/views/Log.php index a1b2eba..1f4037c 100644 --- a/app/views/Log.php +++ b/app/views/Log.php @@ -22,6 +22,7 @@ class Log extends Template $template->prepare('debugMod', DEBUGMOD); $template->prepare('logToLiveTime', LOGTIMOUT); $template->prepare('title', 'Logy'); + $template->prepare('logStats', LogMaintainer::getStats()); $template->prepare('logsFiles', $result); $template->prepare('langMng', $langMng); diff --git a/app/views/templates/log.phtml b/app/views/templates/log.phtml index f8ab45a..43b991d 100644 --- a/app/views/templates/log.phtml +++ b/app/views/templates/log.phtml @@ -23,6 +23,34 @@

get('t_Logs'); ?>

+ +
+
+
+
echo('error'); ?>
+ +
+
+
+
+
echo('warning'); ?>
+ +
+
+
+
+
echo('exeption'); ?>
+ +
+
+
+
+
echo('log'); ?>
+ +
+
+
+