launcher/log.py

43 lines
1 KiB
Python
Raw Normal View History

import io
import logging
import sys
from logging.handlers import RotatingFileHandler
from launcher.sys_config import sys_config
2024-12-08 20:46:44 +08:00
# 配置日志
def setup_logger():
log_level = sys_config.get('log_level')
logger = logging.getLogger("launcher.log")
2024-12-08 20:46:44 +08:00
logger.setLevel(log_level)
# 设置标准输出编码为 UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 控制台输出
console_handler = logging.StreamHandler()
2024-12-08 20:46:44 +08:00
console_handler.setLevel(log_level)
# 文件输出
file_handler = RotatingFileHandler(
2024-12-08 20:46:44 +08:00
"launcher.log", maxBytes=5 * 1024 * 1024, backupCount=3, encoding='utf-8'
)
file_handler.setLevel(logging.INFO)
# 设置格式化器
formatter = logging.Formatter("%(asctime)s [%(levelname)s] - %(message)s")
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# 添加 Handler
logger.addHandler(console_handler)
logger.addHandler(file_handler)
return logger
# 创建全局 Logger 实例
logger = setup_logger()