Selaa lähdekoodia

feat: add APP_LOG configuration and update log viewing to include app logs

Manuel 8 kuukautta sitten
vanhempi
sitoutus
ce74a99cf0
3 muutettua tiedostoa jossa 14 lisäystä ja 3 poistoa
  1. 1 0
      config.py
  2. 1 1
      routes/admin/logs.py
  3. 12 2
      services/system/logs_service.py

+ 1 - 0
config.py

@@ -99,6 +99,7 @@ class Config:
     # Squid settings
     SQUID_LOG = safe_get_env("SQUID_LOG", "/var/log/squid/access.log")
     SQUID_CACHE_LOG = safe_get_env("SQUID_CACHE_LOG", "/var/log/squid/cache.log")
+    APP_LOG = safe_get_env("APP_LOG", "logs/app.log")
     SQUID_HOST = safe_get_env("SQUID_HOST", "127.0.0.1")
     SQUID_PORT = safe_get_env("SQUID_PORT", 3128, var_type=int)
     BLACKLIST_DOMAINS = safe_get_env("BLACKLIST_DOMAINS", "")

+ 1 - 1
routes/admin/logs.py

@@ -14,6 +14,6 @@ def register_routes(bp):
         max_lines = request.args.get("lines", 250, type=int)
         max_lines = min(max(max_lines, 10), 1000)
 
-        log_files = [Config.SQUID_LOG, Config.SQUID_CACHE_LOG]
+        log_files = [Config.SQUID_LOG, Config.SQUID_CACHE_LOG, Config.APP_LOG]
         logs = service_read_logs(log_files, max_lines, debug=bool(current_app.debug))
         return render_template("admin/logs.html", logs=logs, max_lines=max_lines)

+ 12 - 2
services/system/logs_service.py

@@ -3,6 +3,15 @@ import os
 from loguru import logger
 
 
+import re
+
+ANSI_ESCAPE_RE = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
+
+
+def _strip_ansi(line):
+    return ANSI_ESCAPE_RE.sub('', line)
+
+
 def read_logs(log_files, max_lines, debug=False):
     """Read last `max_lines` from each file in `log_files`.
 
@@ -11,9 +20,10 @@ def read_logs(log_files, max_lines, debug=False):
     logs = {}
     for log_file in log_files:
         try:
-            with open(log_file) as f:
+            with open(log_file, errors='replace') as f:
                 lines = f.readlines()
-                logs[os.path.basename(log_file)] = lines[-max_lines:]
+                cleaned = [_strip_ansi(line.rstrip('\n')) for line in lines[-max_lines:]]
+                logs[os.path.basename(log_file)] = cleaned
         except FileNotFoundError:
             logs[os.path.basename(log_file)] = ["Log file not found"]
         except Exception as e: