From e3b36d958d8d004399ed792f4a60fa2373b747df Mon Sep 17 00:00:00 2001 From: Agent Date: Fri, 20 Mar 2026 17:50:43 +0800 Subject: [PATCH] =?UTF-8?q?v1.1.1=20=E4=BF=AE=E5=A4=8D=EF=BC=9AUnboundLoca?= =?UTF-8?q?lError=20logger=20=E4=BD=9C=E7=94=A8=E5=9F=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复问题: - main() 函数内 logger = setup_logging() 导致作用域错误 - 改用 _apply_log_config() 函数更新日志等级,不重新赋值 logger --- src/remote_volume_monitor.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/remote_volume_monitor.py b/src/remote_volume_monitor.py index 2b76f83..e9563e4 100644 --- a/src/remote_volume_monitor.py +++ b/src/remote_volume_monitor.py @@ -51,6 +51,22 @@ def setup_logging(config_path=None): ) return logging.getLogger(__name__) +def _apply_log_config(config_path): + """根据配置文件更新日志等级(不重新创建 logger)""" + if not Path(config_path).exists(): + return + + try: + config = configparser.ConfigParser() + config.read(config_path, encoding='utf-8') + if 'logging' in config and 'level' in config['logging']: + level_str = config['logging']['level'].upper() + log_level = getattr(logging, level_str, logging.INFO) + logger.setLevel(log_level) + logger.info(f"📝 日志等级:{level_str}") + except Exception as e: + pass # 读取失败保持当前等级 + logger = setup_logging() @@ -561,16 +577,16 @@ def main(): sys.exit(1) config.read(config_path, encoding='utf-8') logger.info(f"✓ 已加载:{config_path}") - # 重新初始化日志(使用配置文件中的日志等级) - logger = setup_logging(str(config_path)) + # 重新配置日志等级(使用配置文件中的日志等级) + _apply_log_config(str(config_path)) else: default_config = Path(__file__).parent.parent / 'config' / 'config.ini' if default_config.exists(): config.read(default_config, encoding='utf-8') logger.info(f"✓ 已加载默认:{default_config}") config_path = str(default_config) - # 重新初始化日志(使用配置文件中的日志等级) - logger = setup_logging(config_path) + # 重新配置日志等级(使用配置文件中的日志等级) + _apply_log_config(config_path) else: config['volume'] = {'remote_volume': str(args.volume)}