2024-12-12 07:46:08 +08:00
|
|
|
import io
|
2024-11-28 15:34:49 +08:00
|
|
|
import logging
|
2024-12-12 07:46:08 +08:00
|
|
|
import sys
|
2024-11-28 15:34:49 +08:00
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
|
2024-12-14 12:37:52 +08:00
|
|
|
from launcher.sys_config import sys_config
|
2024-12-08 20:46:44 +08:00
|
|
|
|
|
|
|
|
2024-11-28 15:34:49 +08:00
|
|
|
# 配置日志
|
|
|
|
def setup_logger():
|
2024-12-14 12:37:52 +08:00
|
|
|
log_level = sys_config.get('log_level')
|
|
|
|
|
2024-11-28 15:34:49 +08:00
|
|
|
logger = logging.getLogger("launcher.log")
|
2024-12-08 20:46:44 +08:00
|
|
|
logger.setLevel(log_level)
|
2024-11-28 15:34:49 +08:00
|
|
|
|
2024-12-12 07:46:08 +08:00
|
|
|
# 设置标准输出编码为 UTF-8
|
|
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
|
|
|
2024-11-28 15:34:49 +08:00
|
|
|
# 控制台输出
|
|
|
|
console_handler = logging.StreamHandler()
|
2024-12-08 20:46:44 +08:00
|
|
|
console_handler.setLevel(log_level)
|
2024-11-28 15:34:49 +08:00
|
|
|
|
|
|
|
# 文件输出
|
|
|
|
file_handler = RotatingFileHandler(
|
2024-12-08 20:46:44 +08:00
|
|
|
"launcher.log", maxBytes=5 * 1024 * 1024, backupCount=3, encoding='utf-8'
|
2024-11-28 15:34:49 +08:00
|
|
|
)
|
|
|
|
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()
|