chongming/app/logger/__init__.py

28 lines
876 B
Python
Raw Permalink Normal View History

2026-01-26 09:50:55 +08:00
import logging
import os
from pathlib import Path
def setup_logging(log_config: dict):
log_level = getattr(logging, log_config["level"], logging.INFO)
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# 创建文件夹
log_file_path = Path(log_config["file"])
log_file_dir = log_file_path.parent
if log_file_dir != Path("."):
log_file_dir.mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=log_config["level"],
format=log_format,
handlers=[
logging.FileHandler(log_config["file"]),
logging.StreamHandler()
]
)
print(f"日志系统已配置 - 级别: {logging.getLevelName(log_level)}, 文件: {log_config['file']}")
def get_logger(name: str) -> logging.Logger:
logger = logging.getLogger(name)
return logger