import logging from logging.handlers import RotatingFileHandler log_level = logging.ERROR # 配置日志 def setup_logger(): logger = logging.getLogger("launcher.log") logger.setLevel(log_level) # 控制台输出 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()