fix: 修复日志系统问题,将日志保存到用户目录,修复打包后崩溃问题

This commit is contained in:
ruisu
2025-02-19 17:56:30 +08:00
parent 0b6b3f13aa
commit 98863b8cb5
2 changed files with 63 additions and 77 deletions

View File

@@ -5,44 +5,22 @@ from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QDialog)
from PyQt6.QtCore import Qt, QTimer, QPropertyAnimation
from PyQt6.QtGui import QIcon, QPixmap
import cursor_account_manager as backend
from logger import logging
import traceback
import tempfile
# 设置日志文件路径
def setup_logging():
try:
# 获取应用程序所在目录
if getattr(sys, 'frozen', False):
# 如果是打包后的应用程序
app_dir = os.path.dirname(sys.executable)
else:
# 如果是开发环境
app_dir = os.path.dirname(os.path.abspath(__file__))
# 创建日志目录
log_dir = os.path.join(app_dir, 'logs')
os.makedirs(log_dir, exist_ok=True)
# 设置日志文件路径
log_file = os.path.join(log_dir, 'app.log')
# 配置日志记录
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.FileHandler(log_file, encoding='utf-8'),
logging.StreamHandler()
]
)
logging.info('日志系统初始化成功')
logging.info(f'日志文件路径: {log_file}')
except Exception as e:
print(f"设置日志系统时出错: {str(e)}")
print(traceback.format_exc())
# 创建临时日志文件
temp_log_file = os.path.join(tempfile.gettempdir(), 'cursor_helper_error.log')
def log_error(error_msg):
with open(temp_log_file, 'a', encoding='utf-8') as f:
f.write(f"{error_msg}\n")
try:
from logger import logging
import cursor_account_manager as backend
except Exception as e:
log_error(f"导入模块错误: {str(e)}\n{traceback.format_exc()}")
raise
class SuccessDialog(QDialog):
def __init__(self, message, parent=None):
@@ -525,9 +503,6 @@ class CursorGUI(QMainWindow):
if __name__ == "__main__":
try:
# 初始化日志系统
setup_logging()
# 记录启动信息
logging.info("=== 应用程序启动 ===")
logging.info(f"Python 版本: {sys.version}")
@@ -539,12 +514,15 @@ if __name__ == "__main__":
# 捕获未处理的异常
def handle_exception(exc_type, exc_value, exc_traceback):
error_msg = f"未捕获的异常:\n{traceback.format_exception(exc_type, exc_value, exc_traceback)}"
log_error(error_msg)
logging.error("未捕获的异常:", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
sys.exit(app.exec())
except Exception as e:
logging.error(f"程序启动失败: {str(e)}")
logging.error(traceback.format_exc())
error_msg = f"程序启动失败: {str(e)}\n{traceback.format_exc()}"
log_error(error_msg)
logging.error(error_msg)
raise