优化日志读取与传输效率
This commit is contained in:
@@ -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 .auth import login_required
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
import os
|
import os
|
||||||
@@ -7,6 +7,8 @@ from datetime import datetime
|
|||||||
import platform
|
import platform
|
||||||
import psutil
|
import psutil
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
import gzip
|
||||||
|
import json
|
||||||
|
|
||||||
# 创建系统信息蓝图
|
# 创建系统信息蓝图
|
||||||
system_bp = Blueprint('system', __name__)
|
system_bp = Blueprint('system', __name__)
|
||||||
@@ -75,11 +77,24 @@ def api_wx_logs():
|
|||||||
else:
|
else:
|
||||||
log_file = os.path.join(project_root, 'wx_info.log')
|
log_file = os.path.join(project_root, 'wx_info.log')
|
||||||
|
|
||||||
# 读取日志文件
|
|
||||||
log_content = []
|
log_content = []
|
||||||
if os.path.exists(log_file):
|
if os.path.exists(log_file):
|
||||||
|
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:
|
with open(log_file, 'r', encoding='utf-8', errors='ignore') as f:
|
||||||
# 使用deque获取最后N行
|
|
||||||
log_content = list(deque(f, lines))
|
log_content = list(deque(f, lines))
|
||||||
else:
|
else:
|
||||||
logger.warning(f"日志文件不存在: {log_file}")
|
logger.warning(f"日志文件不存在: {log_file}")
|
||||||
@@ -90,7 +105,7 @@ def api_wx_logs():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"列出目录文件失败: {e}")
|
logger.error(f"列出目录文件失败: {e}")
|
||||||
|
|
||||||
return jsonify({
|
payload = {
|
||||||
"success": True,
|
"success": True,
|
||||||
"data": {
|
"data": {
|
||||||
"log_type": log_type,
|
"log_type": log_type,
|
||||||
@@ -98,7 +113,15 @@ def api_wx_logs():
|
|||||||
"content": log_content,
|
"content": log_content,
|
||||||
"lines": len(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:
|
except Exception as e:
|
||||||
logger.error(f"获取微信日志失败: {e}")
|
logger.error(f"获取微信日志失败: {e}")
|
||||||
return jsonify({"success": False, "error": str(e)}), 500
|
return jsonify({"success": False, "error": str(e)}), 500
|
||||||
|
|||||||
Reference in New Issue
Block a user