Skip to content
Closed
23 changes: 23 additions & 0 deletions module/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@
from rich.theme import Theme
from rich.traceback import Traceback

import time
from pathlib import Path

SECONDS_IN_DAY = 24 * 60 * 60

def cleanup_logs(log_dir: str = "./log", keep_days: int = 7):
"""删除 log_dir 下所有早于 keep_days 的普通文件"""
cutoff_ts = time.time() - keep_days * SECONDS_IN_DAY

log_path = Path(log_dir)
if not log_path.exists():
return # 目录都没有,直接退出

for p in log_path.iterdir():
# 只处理文件;子文件夹一律跳过
if p.is_file():
try:
if p.stat().st_mtime < cutoff_ts:
p.unlink()
logger.info(f"Removed old log finish: {p.name}")
except Exception as e:
logger.warning(f"Failed to delete {p}: {e}")

def empty_function(*args, **kwargs):
pass
Expand Down Expand Up @@ -99,6 +121,7 @@ class RichFileHandler(RichHandler):


def set_file_logger(name=pyw_name):
cleanup_logs("./log", keep_days=7) # 先清理旧日志
if '_' in name:
name = name.split('_', 1)[0]
log_file = f'./log/{datetime.date.today()}_{name}.txt'
Expand Down