import logging import os from logging.handlers import RotatingFileHandler # 创建日志目录(如果不存在) LOG_DIR = "logs" os.makedirs(LOG_DIR, exist_ok=True) # 日志文件路径 LOG_FILE = os.path.join(LOG_DIR, "app.log") # 日志格式 LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" # 配置日志 logging.basicConfig( level=logging.INFO, # 设置日志级别 format=LOG_FORMAT, handlers=[ # 控制台输出 logging.StreamHandler(), # 文件输出,支持日志文件轮转 RotatingFileHandler(LOG_FILE, maxBytes=5 * 1024 * 1024, backupCount=3, encoding="utf-8"), ], ) # 获取日志实例 def get_logger(name: str): """获取带有模块名称的日志记录器""" return logging.getLogger(name)