2024-12-16 14:31:28 +08:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
|
|
|
|
# 创建日志目录(如果不存在)
|
2024-12-18 17:49:49 +08:00
|
|
|
LOG_DIR = "../logs"
|
2024-12-16 14:31:28 +08:00
|
|
|
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(
|
2024-12-18 17:49:49 +08:00
|
|
|
level=logging.DEBUG, # 设置日志级别
|
2024-12-16 14:31:28 +08:00
|
|
|
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)
|