增加查看Gunicorn的日志的按钮和功能
This commit is contained in:
141
gunicorn_tab.py
141
gunicorn_tab.py
@@ -146,6 +146,21 @@ class GunicornTab(QWidget):
|
||||
output_group.setLayout(output_layout)
|
||||
layout.addWidget(output_group)
|
||||
|
||||
# 日志查看组
|
||||
log_group = QGroupBox("日志查看")
|
||||
log_layout = QGridLayout()
|
||||
|
||||
self.view_access_log_btn = QPushButton("查看访问日志")
|
||||
self.view_access_log_btn.clicked.connect(self.view_access_log)
|
||||
log_layout.addWidget(self.view_access_log_btn, 0, 0)
|
||||
|
||||
self.view_error_log_btn = QPushButton("查看错误日志")
|
||||
self.view_error_log_btn.clicked.connect(self.view_error_log)
|
||||
log_layout.addWidget(self.view_error_log_btn, 0, 1)
|
||||
|
||||
log_group.setLayout(log_layout)
|
||||
layout.addWidget(log_group)
|
||||
|
||||
layout.addStretch()
|
||||
self.setLayout(layout)
|
||||
|
||||
@@ -158,7 +173,8 @@ class GunicornTab(QWidget):
|
||||
if self.parent and hasattr(self.parent, 'get_current_config'):
|
||||
config = self.parent.get_current_config()
|
||||
if config:
|
||||
django_path = config.get('django_path', '')
|
||||
# 使用remote_directory而不是django_path
|
||||
django_path = config.get('remote_directory', '')
|
||||
project_name = config.get('project_name', 'myproject')
|
||||
username = config.get('username', 'www-data')
|
||||
|
||||
@@ -172,7 +188,7 @@ class GunicornTab(QWidget):
|
||||
service_content = self.generate_service_file_from_config(config)
|
||||
self.service_editor.setText(service_content)
|
||||
|
||||
logger.info(f"从当前服务器配置加载Gunicorn配置: django_path={django_path}, project_name={project_name}")
|
||||
logger.info(f"从当前服务器配置加载Gunicorn配置: remote_directory={django_path}, project_name={project_name}")
|
||||
else:
|
||||
logger.warning("未找到当前服务器配置")
|
||||
else:
|
||||
@@ -183,6 +199,7 @@ class GunicornTab(QWidget):
|
||||
|
||||
if config and 'servers' in config and len(config['servers']) > 0:
|
||||
server_config = config['servers'][0]
|
||||
# 使用remote_directory
|
||||
django_path = server_config.get('remote_directory', '')
|
||||
project_name = server_config.get('project_name', 'myproject')
|
||||
username = server_config.get('username', 'www-data')
|
||||
@@ -197,7 +214,7 @@ class GunicornTab(QWidget):
|
||||
service_content = self.generate_service_file_from_config(server_config)
|
||||
self.service_editor.setText(service_content)
|
||||
|
||||
logger.info(f"从配置文件加载Gunicorn配置: django_path={django_path}, project_name={project_name}")
|
||||
logger.info(f"从配置文件加载Gunicorn配置: remote_directory={django_path}, project_name={project_name}")
|
||||
except Exception as e:
|
||||
logger.error(f"加载Gunicorn配置失败: {str(e)}")
|
||||
# 不显示警告,避免影响用户体验
|
||||
@@ -209,7 +226,7 @@ class GunicornTab(QWidget):
|
||||
django_path = config.get('remote_directory', '/home/user')
|
||||
|
||||
# 构建完整的项目路径
|
||||
project_path = f"{django_path.rstrip('/')}/{project_name}"
|
||||
project_path = f"{django_path.rstrip('/')}"
|
||||
|
||||
return f"""[Unit]
|
||||
Description=Gunicorn daemon for {project_name}
|
||||
@@ -478,4 +495,118 @@ WantedBy=multi-user.target"""
|
||||
# 使用gunicorn_[project_name].service格式作为服务名称
|
||||
service_name = f"gunicorn_{project_name}"
|
||||
|
||||
self.manage_service("status")
|
||||
self.manage_service("status")
|
||||
|
||||
def view_access_log(self):
|
||||
"""查看Gunicorn访问日志"""
|
||||
if not self.check_ssh_connection():
|
||||
return
|
||||
|
||||
# 获取config.json中的配置信息
|
||||
config = None
|
||||
if self.parent and hasattr(self.parent, 'server_connection_tab'):
|
||||
config = self.parent.server_connection_tab.get_current_config()
|
||||
|
||||
if config:
|
||||
project_path = config.get('remote_directory', '/home/xiaji')
|
||||
else:
|
||||
project_path = '/home/xiaji'
|
||||
|
||||
# 使用动态路径
|
||||
access_log_path = f"{project_path.rstrip('/')}/logs/gunicorn_access.log"
|
||||
|
||||
self.output_text.append(f"正在查看访问日志: {access_log_path}")
|
||||
|
||||
# 获取密码
|
||||
password = self.get_password()
|
||||
if password is None:
|
||||
return
|
||||
|
||||
try:
|
||||
# 首先检查日志文件是否存在,如果不存在则创建
|
||||
log_dir = f"{project_path.rstrip('/')}/logs"
|
||||
check_command = f"sudo test -f {access_log_path} || (sudo mkdir -p {log_dir} && sudo touch {access_log_path} && sudo chmod 644 {access_log_path})"
|
||||
stdin, stdout, stderr = self.parent.ssh_client.exec_command(check_command)
|
||||
stdin.write(password + '\n')
|
||||
stdin.flush()
|
||||
|
||||
# 使用tail命令查看日志的最后50行
|
||||
command = f"sudo tail -n 50 {access_log_path}"
|
||||
stdin, stdout, stderr = self.parent.ssh_client.exec_command(command)
|
||||
stdin.write(password + '\n')
|
||||
stdin.flush()
|
||||
|
||||
output = stdout.read().decode('utf-8')
|
||||
error = stderr.read().decode('utf-8')
|
||||
|
||||
if output:
|
||||
self.output_text.append("=== Gunicorn访问日志 ===")
|
||||
self.output_text.append(output)
|
||||
logger.info(f"成功查看访问日志: {len(output)} 字符")
|
||||
else:
|
||||
self.output_text.append("访问日志为空")
|
||||
|
||||
if error and "sudo" not in error.lower():
|
||||
self.output_text.append(f"错误信息: {error}")
|
||||
logger.error(f"查看访问日志出错: {error}")
|
||||
|
||||
except Exception as e:
|
||||
self.output_text.append(f"查看访问日志失败: {str(e)}")
|
||||
logger.error(f"查看访问日志失败: {str(e)}")
|
||||
|
||||
def view_error_log(self):
|
||||
"""查看Gunicorn错误日志"""
|
||||
if not self.check_ssh_connection():
|
||||
return
|
||||
|
||||
# 获取config.json中的配置信息
|
||||
config = None
|
||||
if self.parent and hasattr(self.parent, 'server_connection_tab'):
|
||||
config = self.parent.server_connection_tab.get_current_config()
|
||||
|
||||
if config:
|
||||
project_path = config.get('remote_directory', '/home/xiaji')
|
||||
else:
|
||||
project_path = '/home/xiaji'
|
||||
|
||||
# 使用动态路径
|
||||
error_log_path = f"{project_path.rstrip('/')}/logs/gunicorn_error.log"
|
||||
|
||||
self.output_text.append(f"正在查看错误日志: {error_log_path}")
|
||||
|
||||
# 获取密码
|
||||
password = self.get_password()
|
||||
if password is None:
|
||||
return
|
||||
|
||||
try:
|
||||
# 首先检查日志文件是否存在,如果不存在则创建
|
||||
log_dir = f"{project_path.rstrip('/')}/logs"
|
||||
check_command = f"sudo test -f {error_log_path} || (sudo mkdir -p {log_dir} && sudo touch {error_log_path} && sudo chmod 644 {error_log_path})"
|
||||
stdin, stdout, stderr = self.parent.ssh_client.exec_command(check_command)
|
||||
stdin.write(password + '\n')
|
||||
stdin.flush()
|
||||
|
||||
# 使用tail命令查看日志的最后50行
|
||||
command = f"sudo tail -n 50 {error_log_path}"
|
||||
stdin, stdout, stderr = self.parent.ssh_client.exec_command(command)
|
||||
stdin.write(password + '\n')
|
||||
stdin.flush()
|
||||
|
||||
output = stdout.read().decode('utf-8')
|
||||
error = stderr.read().decode('utf-8')
|
||||
|
||||
if output:
|
||||
self.output_text.append("=== Gunicorn错误日志 ===")
|
||||
self.output_text.append(output)
|
||||
logger.info(f"成功查看错误日志: {len(output)} 字符")
|
||||
else:
|
||||
self.output_text.append("错误日志为空")
|
||||
|
||||
if error and "sudo" not in error.lower():
|
||||
self.output_text.append(f"错误信息: {error}")
|
||||
logger.error(f"查看错误日志出错: {error}")
|
||||
|
||||
except Exception as e:
|
||||
self.output_text.append(f"查看错误日志失败: {str(e)}")
|
||||
logger.error(f"查看错误日志失败: {str(e)}")
|
||||
Reference in New Issue
Block a user