youfool-devops-gd-jdk21/ERROR_CAPTURE_GUIDE.md

228 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 错误捕获和日志系统使用指南
## 🎯 系统功能概述
本系统已集成了完整的错误捕获和日志记录功能,能够自动捕获、分类保存和查看各种类型的错误信息。
## 📁 错误日志分类
### 自动分类保存
所有错误信息会自动保存到 `logs/errors/` 目录,按类型分类:
1. **启动错误** (`startup-error-*.log`)
- 应用启动失败
- Spring Boot初始化异常
- 配置加载错误
2. **运行时错误** (`runtime-error-*.log`)
- 空指针异常
- 非法参数异常
- Java模块访问异常
3. **数据库错误** (`database-error-*.log`)
- 数据库连接失败
- SQL执行异常
- MyBatis持久化错误
4. **业务错误** (`business-error-*.log`)
- 监控服务异常
- 微信通知发送失败
- 业务逻辑处理错误
5. **启动信息** (`startup-info.log`)
- 应用启动过程记录
- 数据库健康检查结果
- 系统配置信息
## 🚀 启动和查看错误
### 启动应用
```cmd
# 使用增强的调试脚本启动(推荐)
start_debug.bat
# 或使用简单启动脚本
start_with_logging.bat
```
### 查看错误信息
#### 1. 文件系统方式
```cmd
# 查看所有错误日志文件
dir logs\errors\*.log
# 查看启动错误(最重要)
type logs\errors\startup-error-*.log
# 查看启动信息
type logs\errors\startup-info.log
# 查看最新的运行时错误
type logs\errors\runtime-error-*.log
```
#### 2. Web接口方式
应用启动后,访问以下接口:
- **错误状态概览**: http://localhost:8080/api/error-logs/status
- **错误文件列表**: http://localhost:8080/api/error-logs/files
- **读取具体文件**: http://localhost:8080/api/error-logs/content?fileName=startup-error-2025-08-12.log
## 🔍 错误诊断流程
### 1. 应用启动失败
```cmd
# 运行调试脚本
start_debug.bat
# 检查启动错误日志
type logs\errors\startup-error-*.log
# 检查启动信息
type logs\errors\startup-info.log
# 检查数据库连接
netstat -an | findstr 5432
```
### 2. 运行时错误
```cmd
# 查看运行时错误日志
type logs\errors\runtime-error-*.log
# 查看业务错误日志
type logs\errors\business-error-*.log
# 通过Web接口查看
# 访问: http://localhost:8080/api/error-logs/status
```
### 3. 数据库相关错误
```cmd
# 查看数据库错误日志
type logs\errors\database-error-*.log
# 查看数据库健康检查结果
type logs\errors\startup-info.log | findstr "数据库\|健康检查"
```
## 📊 Web错误监控界面
### API接口说明
1. **GET /api/error-logs/status**
- 获取错误统计信息
- 返回各类错误数量和日志文件大小
2. **GET /api/error-logs/files**
- 获取所有错误日志文件列表
- 包含文件大小、修改时间等信息
3. **GET /api/error-logs/content?fileName=xxx&lines=100**
- 读取指定日志文件内容
- 默认显示最后100行
4. **DELETE /api/error-logs/cleanup?keepDays=7**
- 清理超过指定天数的旧日志文件
- 默认保留7天
### 使用示例
```bash
# 获取错误状态
curl http://localhost:8080/api/error-logs/status
# 获取文件列表
curl http://localhost:8080/api/error-logs/files
# 读取启动错误日志
curl "http://localhost:8080/api/error-logs/content?fileName=startup-error-2025-08-12.log&lines=50"
```
## 🛠️ 故障排除
### 常见问题及解决方案
#### 1. Java模块系统兼容性错误
```
错误: java.lang.reflect.InaccessibleObjectException
解决: 应用已自动设置所需的JVM参数无需手动处理
```
#### 2. 数据库连接失败
```
错误: 数据库连接异常
检查:
- PostgreSQL是否启动 (netstat -an | findstr 5432)
- 数据库是否已创建 (devops_gd)
- 用户名密码是否正确 (postgres/123456)
```
#### 3. 端口占用
```
错误: 端口8080已被占用
解决: netstat -ano | findstr 8080 找到进程并结束
```
#### 4. Maven参数解析错误
```
错误: Unknown lifecycle phase
解决: 使用提供的批处理脚本启动避免PowerShell参数解析问题
```
## 📝 日志维护
### 自动清理
```bash
# 通过Web接口清理7天前的日志
curl -X DELETE "http://localhost:8080/api/error-logs/cleanup?keepDays=7"
```
### 手动清理
```cmd
# 删除所有错误日志(谨慎操作)
del logs\errors\*.log
# 删除超过7天的日志文件
forfiles /p logs\errors /s /m *.log /d -7 /c "cmd /c del @path"
```
## 🔧 系统配置
### 错误日志配置
- **位置**: `src/main/resources/logback-spring.xml`
- **错误工具类**: `com.chinaweal.youfool.devops.util.ErrorLogUtils`
- **全局异常处理**: `com.chinaweal.youfool.devops.config.GlobalExceptionHandler`
### 数据库健康检查
- **组件**: `com.chinaweal.youfool.devops.config.DatabaseHealthChecker`
- **执行时机**: 应用启动完成后自动执行
- **检查内容**: 数据库连接、基本信息获取
## 📋 最佳实践
1. **定期检查错误状态**
- 访问 http://localhost:8080/api/error-logs/status
- 关注错误数量变化趋势
2. **及时处理启动错误**
- 启动失败时优先查看 `startup-error-*.log`
- 根据错误信息进行针对性修复
3. **监控数据库健康**
- 关注 `database-error-*.log` 中的连接失败记录
- 定期检查数据库健康检查结果
4. **日志文件管理**
- 定期清理旧日志文件避免磁盘空间不足
- 保留重要的错误日志用于问题分析
## 🆘 紧急情况处理
如果遇到严重错误无法启动:
1. **查看最新的启动错误日志**
2. **检查数据库服务状态**
3. **确认Java环境和Maven配置**
4. **使用Web接口如果应用部分可用获取详细错误信息**
5. **将错误日志文件提供给开发人员进行进一步分析**