import logging
import os
from logging.handlers import TimedRotatingFileHandler

from app.config.config_reader import log_dir


file_suffix = "%Y-%m-%d"
os.makedirs(log_dir, exist_ok=True)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

#所有http请求的日志
logger_http = logging.getLogger("api_log")
logger_http.setLevel(logging.DEBUG)
# 创建一个TimedRotatingFileHandler，指定日志文件名、轮转周期和备份数量
api_log_file = os.path.join(log_dir, 'api.log')
api_handler = TimedRotatingFileHandler(api_log_file, when="midnight", interval=1, backupCount=30)
api_handler.setFormatter(formatter)
api_handler.suffix = file_suffix  # 日志文件的后缀为日期格式
logger_http.addHandler(api_handler)

#所有sqlalchemy打印的日志
logger_sql = logging.getLogger("sqlalchemy.engine.Engine")
logger_sql.setLevel(logging.DEBUG)
# 创建文件处理器并设置级别
sql_log_file = os.path.join(log_dir, 'sql.log')
sql_handler = TimedRotatingFileHandler(sql_log_file, when="midnight", interval=1, backupCount=30)
sql_handler.setFormatter(formatter)
sql_handler.suffix = file_suffix  # 日志文件的后缀为日期格式
logger_sql.addHandler(sql_handler)


