ai-chat-ui/server/docs/logging.md

101 lines
2.5 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.

# 日志系统文档
## 概述
本项目实现了统一的后端日志系统,为所有接口提供了详细的日志记录功能,包括请求、响应、错误等状态。
## 日志系统结构
### 1. logger.js
- 统一日志管理模块
- 支持多种日志级别ERROR, WARN, INFO, DEBUG
- 支持控制台和文件双重输出
- 包含HTTP请求专用日志方法
### 2. 日志文件位置
- 存储在 `server/logs/` 目录
- 按日期命名:`server-YYYY-MM-DD.log`
- JSON格式便于查询和分析
## 日志级别
- `ERROR`: 错误和异常情况
- `WARN`: 警告和潜在问题
- `INFO`: 重要操作和状态信息
- `DEBUG`: 详细调试信息
## 接口日志详情
### 1. 对话接口 (`/api/chat-ui/chat`)
- 记录请求代理开始和结束
- 记录目标API响应状态
- 记录代理错误
### 2. 模型列表接口 (`/api/chat-ui/models`)
- 记录请求基本信息
- 记录返回的模型数量
### 3. 对话管理接口 (`/api/chat-ui/conversations/*`)
- 记录操作类型(获取/创建/删除)
- 记录对话ID
- 记录操作结果
### 4. 文件上传接口 (`/api/chat-ui/upload`)
- 记录上传文件信息(名称、大小、类型)
- 记录上传结果和生成的URL
### 5. 停止生成接口 (`/api/chat-ui/stop/*`)
- 记录停止请求的ID
- 记录操作结果
### 6. HTTP通用日志
- 记录所有请求的方法、URL、状态码
- 记录请求耗时
- 记录客户端IP和User-Agent
- 根据状态码自动分类日志级别
## 使用方法
### 环境变量
```bash
LOG_LEVEL=DEBUG # 设置日志级别默认为INFO
```
### 在代码中使用
```javascript
const { logger } = require('./logger');
logger.info('Some info message', { additional: 'data' });
logger.error('Error occurred', { error: error.message });
logger.debug('Debug info', { variable: value });
```
## 日志示例
### HTTP请求日志
```json
{
"timestamp": "2026-03-03T03:26:58.631Z",
"level": "INFO",
"message": "HTTP GET /api/chat-ui/models 200 15ms",
"method": "GET",
"url": "/api/chat-ui/models",
"statusCode": 200,
"duration": "15ms",
"userAgent": "Mozilla/5.0...",
"ip": "::1"
}
```
### 特定接口日志
```json
{
"timestamp": "2026-03-03T03:27:12.123Z",
"level": "INFO",
"message": "Successfully uploaded file",
"filename": "1642342342-file.jpg",
"originalName": "file.jpg",
"url": "http://localhost:3000/uploads/1642342342-file.jpg",
"size": 123456
}
```