stany.ferer преди 3 месеца
родител
ревизия
f9617832bf
променени са 1 файла, в които са добавени 42 реда и са изтрити 39 реда
  1. 42 39
      core/class/serverLog.class.php

+ 42 - 39
core/class/serverLog.class.php

@@ -3,18 +3,20 @@
 class serverLog {
 
     public static function printLog(string $_log){
-        if (serverLog::startsWithIpDate($_log)) {
+        if (serverLog::detectLogsAttempts($_log)) {
             self::printLogAttempts($_log);
+        } elseif (serverLog::detectApacheLog($_log)) {
+            self::printColoredApacheLog($_log );
         } elseif (stripos($_log, 'error') !== false) {
-            echo '<div class="error">' . $_log . '</div>';
+            echo '<div style="color: salmon;">' . $_log . '</div>';
         } elseif (stripos($_log, 'fatal') !== false) {
-            echo '<div class="error">' . $_log . '</div>';
+            echo '<div style="color: #FF5252;">' . $_log . '</div>';
         } elseif (stripos($_log, 'warn') !== false OR stripos($_log, 'alert') !== false) {
-            echo '<div class="warning">' . $_log . '</div>';
+            echo '<div style="color: orange;">' . $_log . '</div>';
         } elseif (stripos($_log, 'notice') !== false) {
-            echo '<div class="notice">' . $_log . '</div>';
+            echo '<div style="color: white;">' . $_log . '</div>';
         } else {
-            echo '<div class="grey">' . $_log . '</div>';
+            echo '<div style="color: grey;">' . $_log . '</div>';
         }
     }
 
@@ -24,7 +26,7 @@ class serverLog {
         self::printLog($log);
     }
 
-    private static function startsWithIpDate(string $str): bool {
+    private static function detectLogsAttempts(string $str): bool {
         return preg_match(
             '/^\d{1,3}(\.\d{1,3}){3},\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},/',
             $str
@@ -32,28 +34,41 @@ class serverLog {
     }
 
     public static function printLogAttempts(string $_log){
-        if (self::startsWithIpDate($_log)) {
-            $parts = explode(',', $_log, 4);
-            if (count($parts) === 4) {
-                echo '<div class="notice">'
-                    . '<span style="color:#4FC3F7;">' . htmlspecialchars($parts[0]) . '</span>, '
-                    . '<span style="color:#81C784;">' . htmlspecialchars($parts[1]) . '</span>, '
-                    . '<span style="color:#FFD54F;">' . htmlspecialchars($parts[2]) . '</span>, '
-                    . '<span style="color:#FF8A65;">' . htmlspecialchars($parts[3]) . '</span>'
-                    . '</div>';
-                return;
-            }
+        $parts = explode(',', $_log, 4);
+        if (count($parts) === 4) {
+            echo '<div class="notice">'
+                . '<span style="color:#4FC3F7;">' . htmlspecialchars($parts[0]) . '</span>, '
+                . '<span style="color:#81C784;">' . htmlspecialchars($parts[1]) . '</span>, '
+                . '<span style="color:#FFD54F;">' . htmlspecialchars($parts[2]) . '</span>, '
+                . '<span style="color:#FF8A65;">' . htmlspecialchars($parts[3]) . '</span>'
+                . '</div>';
+            return;
         }
-        elseif (stripos($_log, 'error') !== false) {
-            echo '<div class="error">' . $_log . '</div>';
-        } elseif (stripos($_log, 'fatal') !== false) {
-            echo '<div class="error">' . $_log . '</div>';
-        } elseif (stripos($_log, 'warn') !== false OR stripos($_log, 'alert') !== false) {
-            echo '<div class="warning">' . $_log . '</div>';
-        } elseif (stripos($_log, 'notice') !== false) {
-            echo '<div class="notice">' . $_log . '</div>';
+    }
+
+    private static function detectApacheLog(string $str): bool {
+        return preg_match(
+            '/^\[?(?<date>[A-Za-z]{3} [A-Za-z]{3} \d{2} [\d:.]+ \d{4})\]? \[(?<type>[^\]]+)\] \[pid (?<pid>\d+):tid (?<tid>\d+)\] \[client (?<client>[^\]]+)\] (?<msg>.+?)(?:, referer: (?<referer>.+))?$/',
+            $str
+        ) === 1;
+    }
+
+    public static function printColoredApacheLog(string $line) {
+        $regex = '/^\[?(?<date>[A-Za-z]{3} [A-Za-z]{3} \d{2} [\d:.]+ \d{4})\]? \[(?<type>[^\]]+)\] \[pid (?<pid>\d+):tid (?<tid>\d+)\] \[client (?<client>[^\]]+)\] (?<msg>.+?)(?:, referer: (?<referer>.+))?$/';
+
+        if (preg_match($regex, $line, $matches)) {
+            echo '<div class="notice">'
+                . '<span style="color:#4FC3F7;">[' . htmlspecialchars($matches['date']) . ']</span> '
+                . '<span style="color:#FFD54F;">[' . htmlspecialchars($matches['type']) . ']</span> '
+                . '<span style="color:#BDBDBD;">[pid ' . htmlspecialchars($matches['pid']) . ':tid ' . htmlspecialchars($matches['tid']) . ']</span> '
+                . '<span style="color:#81C784;">[client ' . htmlspecialchars($matches['client']) . ']</span> '
+                . '<span style="color:#FF8A65;">' . htmlspecialchars($matches['msg']) . '</span>';
+            if (!empty($matches['referer'])) {
+                echo ' <span style="color:#90CAF9;">referer: ' . htmlspecialchars($matches['referer']) . '</span>';
+            }
+            echo '</div>';
         } else {
-            echo '<div class="grey">' . $_log . '</div>';
+            echo '<div style="color:grey;">' . htmlspecialchars($line) . '</div>';
         }
     }
 
@@ -100,10 +115,6 @@ class serverLog {
             <style>
                 body { font-family: monospace; background: black !important; padding: 20px; }
                 div { font-size: 0.8em; }
-                .error { color: salmon; }
-                .warning { color: orange; }
-                .notice { color: white; }
-                .grey { color: grey; }
                 pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
             </style>
             <link rel="stylesheet" href="' . cache::printFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
@@ -140,10 +151,6 @@ class serverLog {
             <style>
                 body { font-family: monospace; background: black !important; padding: 20px; }
                 div { font-size: 0.8em; }
-                .error { color: salmon; }
-                .warning { color: orange; }
-                .notice { color: white; }
-                .grey { color: grey; }
                 pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
             </style>
             <link rel="stylesheet" href="' . cache::printFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
@@ -180,10 +187,6 @@ class serverLog {
             <style>
                 body { font-family: monospace; background: black !important; padding: 20px; }
                 div { font-size: 0.8em; }
-                .error { color: salmon; }
-                .warning { color: orange; }
-                .notice { color: white; }
-                .grey { color: grey; }
                 pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
             </style>
             <link rel="stylesheet" href="' . cache::printFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">