v1.1.1 修复:UnboundLocalError logger 作用域问题

修复问题:
- main() 函数内 logger = setup_logging() 导致作用域错误
- 改用 _apply_log_config() 函数更新日志等级,不重新赋值 logger
This commit is contained in:
2026-03-20 17:50:43 +08:00
parent 3b6a5fe2a0
commit e3b36d958d

View File

@@ -51,6 +51,22 @@ def setup_logging(config_path=None):
) )
return logging.getLogger(__name__) 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() logger = setup_logging()
@@ -561,16 +577,16 @@ def main():
sys.exit(1) sys.exit(1)
config.read(config_path, encoding='utf-8') config.read(config_path, encoding='utf-8')
logger.info(f"✓ 已加载:{config_path}") logger.info(f"✓ 已加载:{config_path}")
# 重新初始化日志(使用配置文件中的日志等级) # 重新配置日志等级(使用配置文件中的日志等级)
logger = setup_logging(str(config_path)) _apply_log_config(str(config_path))
else: else:
default_config = Path(__file__).parent.parent / 'config' / 'config.ini' default_config = Path(__file__).parent.parent / 'config' / 'config.ini'
if default_config.exists(): if default_config.exists():
config.read(default_config, encoding='utf-8') config.read(default_config, encoding='utf-8')
logger.info(f"✓ 已加载默认:{default_config}") logger.info(f"✓ 已加载默认:{default_config}")
config_path = str(default_config) config_path = str(default_config)
# 重新初始化日志(使用配置文件中的日志等级) # 重新配置日志等级(使用配置文件中的日志等级)
logger = setup_logging(config_path) _apply_log_config(config_path)
else: else:
config['volume'] = {'remote_volume': str(args.volume)} config['volume'] = {'remote_volume': str(args.volume)}