40 lines
996 B
Python
40 lines
996 B
Python
import io
|
|
import logging
|
|
import sys
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
log_level = logging.INFO
|
|
|
|
|
|
# 配置日志
|
|
def setup_logger():
|
|
logger = logging.getLogger("launcher.log")
|
|
logger.setLevel(log_level)
|
|
|
|
# 设置标准输出编码为 UTF-8
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
|
|
# 控制台输出
|
|
console_handler = logging.StreamHandler()
|
|
console_handler.setLevel(log_level)
|
|
|
|
# 文件输出
|
|
file_handler = RotatingFileHandler(
|
|
"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()
|