diff --git a/admin/dashboard/blueprints/system.py b/admin/dashboard/blueprints/system.py index 6119855..3727ea8 100644 --- a/admin/dashboard/blueprints/system.py +++ b/admin/dashboard/blueprints/system.py @@ -1,4 +1,4 @@ -from flask import Blueprint, render_template, jsonify, request, send_from_directory, current_app +from flask import Blueprint, render_template, jsonify, request, send_from_directory, current_app, Response from .auth import login_required from loguru import logger import os @@ -7,6 +7,8 @@ from datetime import datetime import platform import psutil from collections import deque +import gzip +import json # 创建系统信息蓝图 system_bp = Blueprint('system', __name__) @@ -75,12 +77,25 @@ def api_wx_logs(): else: log_file = os.path.join(project_root, 'wx_info.log') - # 读取日志文件 log_content = [] if os.path.exists(log_file): - with open(log_file, 'r', encoding='utf-8', errors='ignore') as f: - # 使用deque获取最后N行 - log_content = list(deque(f, lines)) + try: + chunk_size = 8192 + with open(log_file, 'rb') as f: + f.seek(0, os.SEEK_END) + size = f.tell() + buffer = b"" + pos = size + while pos > 0 and buffer.count(b'\n') <= lines: + read_size = chunk_size if pos >= chunk_size else pos + pos -= read_size + f.seek(pos) + buffer = f.read(read_size) + buffer + log_content = [b.decode('utf-8', errors='ignore') for b in buffer.splitlines()[-lines:]] + except Exception as e: + logger.error(f"高效读取日志失败,回退到常规方式: {e}") + with open(log_file, 'r', encoding='utf-8', errors='ignore') as f: + log_content = list(deque(f, lines)) else: logger.warning(f"日志文件不存在: {log_file}") # 尝试列出项目根目录下的所有日志文件,帮助调试 @@ -90,7 +105,7 @@ def api_wx_logs(): except Exception as e: logger.error(f"列出目录文件失败: {e}") - return jsonify({ + payload = { "success": True, "data": { "log_type": log_type, @@ -98,7 +113,15 @@ def api_wx_logs(): "content": log_content, "lines": len(log_content) } - }) + } + accept = request.headers.get('Accept-Encoding', '') + if 'gzip' in accept.lower(): + body = json.dumps(payload, ensure_ascii=False).encode('utf-8') + gz = gzip.compress(body, compresslevel=6) + resp = Response(gz, mimetype='application/json') + resp.headers['Content-Encoding'] = 'gzip' + return resp + return jsonify(payload) except Exception as e: logger.error(f"获取微信日志失败: {e}") return jsonify({"success": False, "error": str(e)}), 500