feat: 改进日志系统和文档
1. 改进日志系统 - 添加日志文件轮转功能 - 优化日志文件路径处理 - 重定向控制台输出 - 添加错误处理机制 2. 更新文档 - 添加日志系统说明 - 更新最近更新记录
This commit is contained in:
parent
bb5e014a9b
commit
b19f1d1d11
@ -164,6 +164,25 @@ save_location/
|
||||
- 包含配置文件和分类规则文件
|
||||
- 支持的 Python 版本:3.10+
|
||||
|
||||
## 📝 日志说明
|
||||
|
||||
应用程序运行时的所有日志都保存在 `logs` 目录下:
|
||||
- 日志文件:`llmclipboard.log`
|
||||
- 日志级别:INFO 及以上
|
||||
- 日志轮转:单个文件最大 5MB,保留 3 个备份文件
|
||||
- 日志内容:包含所有控制台输出和错误信息
|
||||
|
||||
### 日志文件位置
|
||||
- 开发环境:项目根目录下的 `logs` 文件夹
|
||||
- 打包环境:可执行文件所在目录的 `logs` 文件夹
|
||||
|
||||
### 日志内容说明
|
||||
- 应用程序启动和关闭信息
|
||||
- 剪贴板操作记录
|
||||
- 文件保存操作记录
|
||||
- 错误和异常信息
|
||||
- 调试信息(DEBUG 级别,默认不记录)
|
||||
|
||||
## 🔄 最近更新
|
||||
|
||||
### 2025-01-15
|
||||
@ -181,6 +200,23 @@ save_location/
|
||||
- 确保 UTF-8 编码支持
|
||||
- 优化日志格式和可读性
|
||||
|
||||
### 2025-01-16
|
||||
- 添加应用程序打包支持
|
||||
- 使用 PyInstaller 打包为独立可执行文件
|
||||
- 添加打包配置文件 `llmclipboard.spec`
|
||||
- 优化打包后的文件结构和依赖管理
|
||||
|
||||
- 改进日志系统
|
||||
- 添加日志文件轮转功能,单个文件最大 5MB
|
||||
- 优化日志文件路径处理,支持开发和打包环境
|
||||
- 重定向控制台输出到日志文件
|
||||
- 添加错误处理和临时日志备份机制
|
||||
|
||||
- 文档更新
|
||||
- 添加打包说明和配置文档
|
||||
- 补充日志系统使用说明
|
||||
- 更新项目结构说明
|
||||
|
||||
## 🤝 贡献指南
|
||||
|
||||
1. Fork 项目
|
||||
|
||||
@ -9,9 +9,24 @@ import keyboard
|
||||
import threading
|
||||
import sys
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
from .gui import MainWindow
|
||||
from .document_processor import DocumentProcessor
|
||||
from llmclipboard.gui import MainWindow
|
||||
from llmclipboard.document_processor import DocumentProcessor
|
||||
|
||||
# 重定向标准输出和标准错误到日志
|
||||
class StreamToLogger:
|
||||
def __init__(self, logger, level):
|
||||
self.logger = logger
|
||||
self.level = level
|
||||
self.linebuf = ''
|
||||
|
||||
def write(self, buf):
|
||||
for line in buf.rstrip().splitlines():
|
||||
self.logger.log(self.level, line.rstrip())
|
||||
|
||||
def flush(self):
|
||||
pass
|
||||
|
||||
class TextCaptureService:
|
||||
def __init__(self):
|
||||
@ -33,12 +48,62 @@ class TextCaptureService:
|
||||
fallback=os.path.expanduser('~/Documents'))
|
||||
|
||||
def setup_logging(self):
|
||||
logging.basicConfig(
|
||||
filename='text_capture.log',
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
try:
|
||||
# 获取应用程序根目录
|
||||
if getattr(sys, 'frozen', False):
|
||||
# 如果是打包后的环境
|
||||
app_root = os.path.dirname(sys.executable)
|
||||
else:
|
||||
# 如果是开发环境
|
||||
app_root = os.path.dirname(os.path.dirname(__file__))
|
||||
|
||||
# 设置日志目录
|
||||
log_dir = os.path.join(app_root, 'logs')
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
# 设置日志文件路径
|
||||
log_file = os.path.join(log_dir, 'llmclipboard.log')
|
||||
|
||||
# 创建 RotatingFileHandler
|
||||
file_handler = RotatingFileHandler(
|
||||
log_file,
|
||||
maxBytes=5*1024*1024, # 5MB
|
||||
backupCount=3,
|
||||
encoding='utf-8'
|
||||
)
|
||||
|
||||
# 设置日志格式
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
# 配置根日志记录器
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.INFO)
|
||||
|
||||
# 移除所有现有的处理器
|
||||
for handler in root_logger.handlers[:]:
|
||||
root_logger.removeHandler(handler)
|
||||
|
||||
# 添加新的处理器
|
||||
root_logger.addHandler(file_handler)
|
||||
|
||||
# 创建应用程序日志记录器
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# 重定向标准输出和标准错误到日志
|
||||
sys.stdout = StreamToLogger(self.logger, logging.INFO)
|
||||
sys.stderr = StreamToLogger(self.logger, logging.ERROR)
|
||||
|
||||
self.logger.info('日志系统初始化完成')
|
||||
self.logger.info(f'日志文件路径: {log_file}')
|
||||
except Exception as e:
|
||||
# 如果日志设置失败,尝试写入到临时目录
|
||||
import tempfile
|
||||
temp_dir = tempfile.gettempdir()
|
||||
fallback_log = os.path.join(temp_dir, 'llmclipboard_error.log')
|
||||
with open(fallback_log, 'a', encoding='utf-8') as f:
|
||||
f.write(f'{time.strftime("%Y-%m-%d %H:%M:%S")} - 日志初始化失败: {str(e)}\n')
|
||||
|
||||
def is_valid_content(self, content):
|
||||
"""验证内容是否有效"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user