import os
import logging
from logging.handlers import TimedRotatingFileHandler

# 确定日志文件路径
log_dir = 'log'
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, 'app.log')

# 配置日志记录器
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# 创建 TimedRotatingFileHandler 处理程序
handler = TimedRotatingFileHandler(filename=log_file, when='midnight', interval=1, backupCount=7)
handler.suffix = '%Y-%m-%d.log'  # 添加日期后缀

# 设置日志记录格式
# formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d]')
handler.setFormatter(formatter)

# 将处理程序添加到日志记录器
logger.addHandler(handler)
