|
|
@@ -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:
|