日志系统功能说明

1. 日志文件结构

 
/opt/gradio_app/chatbot_logs/
├── chatbot.log          # 应用运行日志
├── error.log           # 错误日志
├── api_calls.log       # API调用详细日志
├── chat_history.jsonl  # 结构化聊天历史(JSON格式)
└── stats.json         # 系统统计数据
 
 

2. 记录的详细信息

  • 每次查询的完整信息:消息、响应、时间戳
  • API调用详情:耗时、成功/失败状态
  • 错误追踪:完整的错误堆栈
  • 系统统计:查询次数、成功率、平均响应时间

3. 新增功能

  1. 统计面板:在Web界面中添加了统计查看标签页
  2. JSONL格式日志:方便机器处理和分析
  3. 自动统计:实时统计查询次数、成功率等
  4. 日志轮转:避免日志文件过大

4. 查看日志的命令

 
# 实时查看应用日志
tail -f /opt/gradio_app/chatbot_logs/chatbot.log

# 查看API调用日志
tail -f /opt/gradio_app/chatbot_logs/api_calls.log

# 查看聊天历史(JSON格式)
tail -n 5 /opt/gradio_app/chatbot_logs/chat_history.jsonl | python3 -m json.tool

# 查看系统统计
cat /opt/gradio_app/chatbot_logs/stats.json | python3 -m json.tool

# 查看今日聊天数量
grep -c "$(date +%Y-%m-%d)" /opt/gradio_app/chatbot_logs/chat_history.jsonl
 
 

5. 日志示例

chat_history.jsonl 中的一条记录:
 
{
  "query_id": "chat_1741590000_1234",
  "message": "解释量子力学",
  "timestamp": "2025-03-10T14:30:00.123456",
  "status": "success",
  "history_length": 2,
  "response": "量子力学是研究微观粒子运动规律的物理学分支...",
  "response_length": 450,
  "duration": 1.23,
  "api_call": {
    "success": true,
    "duration": 1.2,
    "model": "gpt-3.5-turbo"
  }
}
 
 
api_calls.log 中的记录:
 
2025-03-10 14:30:00 | ID:chat_1741590000_1234 | 消息:'解释量子力学' | 耗时:1.20s | 成功
 
 

6. 启动服务

 
# 进入目录
cd /opt/gradio_app

# 停止旧服务
pkill -f "python3.*7860"

# 启动新服务
nohup python3 chatbot_app.py > /opt/gradio_app/chatbot_run.log 2>&1 &

# 查看启动日志
tail -f /opt/gradio_app/chatbot_run.log
 
 
这个修改后的代码提供了完整的日志记录功能,您可以:
  1. 实时监控聊天机器人的运行状态
  2. 查看每次对话的详细记录
  3. 分析API调用性能
  4. 查看系统统计数据
  5. 快速排查问题
 
 
 
 
 
 
 
 
 

作者 littlepudding

奇瑞汽车,车载智能语音开发

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注