改进日志输出:显示执行目录、命令和详细错误信息

This commit is contained in:
孙小云 2026-01-09 18:19:11 +08:00
parent 0fa42b1b2c
commit 3803cec03b
1 changed files with 8 additions and 3 deletions

View File

@ -40,7 +40,10 @@ class Deployer:
def run_command(self, cmd, cwd=None, timeout=600): def run_command(self, cmd, cwd=None, timeout=600):
"""执行命令""" """执行命令"""
cwd_str = str(cwd) if cwd else "当前目录"
self.logger.info(f"执行目录: {cwd_str}")
self.logger.info(f"执行命令: {cmd}") self.logger.info(f"执行命令: {cmd}")
try: try:
result = subprocess.run( result = subprocess.run(
cmd, cmd,
@ -52,15 +55,17 @@ class Deployer:
) )
if result.stdout: if result.stdout:
self.logger.debug(f"输出: {result.stdout}") self.logger.debug(f"标准输出:\n{result.stdout}")
if result.returncode != 0: if result.returncode != 0:
self.logger.error(f"命令执行失败: {result.stderr}") self.logger.error(f"命令执行失败 (退出码: {result.returncode})")
if result.stderr:
self.logger.error(f"错误输出:\n{result.stderr}")
return False return False
return True return True
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
self.logger.error(f"命令执行超时: {cmd}") self.logger.error(f"命令执行超时 (超时时间: {timeout}秒)")
return False return False
except Exception as e: except Exception as e:
self.logger.error(f"命令执行异常: {e}") self.logger.error(f"命令执行异常: {e}")