28 lines
876 B
Python
28 lines
876 B
Python
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
|