summaryapi / utils /logging.py
quyip
fix
d5f14ef
raw
history blame contribute delete
740 Bytes
from logging.handlers import RotatingFileHandler
import logging
log_file = '/.cache/app.log'
# 配置 RotatingFileHandler
handler = RotatingFileHandler(log_file, maxBytes=100 * 1024 * 1024, backupCount=3) # 100MG
handler.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger = logging.getLogger()
logger.addHandler(handler)
def read_last_n_logs(n, level='ERROR'):
error_logs = []
with open(log_file, 'r') as file:
lines = file.readlines()[-n:]
# 检查每行日志的级别,只保留 ERROR 级别的日志
for line in lines:
if level in line:
error_logs.append(line.strip())
return error_logs