钉钉 AI 客服:日志分析实战

2 阅读1分钟

钉钉 AI 客服:日志分析实战

日志分析是优化 AI 客服的重要手段。


一、日志类型

类型内容
对话日志用户问题、AI 回复
错误日志异常信息、堆栈
性能日志响应时间、资源
访问日志请求路径、IP

二、日志收集

2.1 结构化日志

function log(level, message, data = {}) {
  console.log(JSON.stringify({
    timestamp: new Date().toISOString(),
    level,
    message,
    ...data
  }));
}

// 使用
log('info', 'Chat request', { userId: '123', message: '你好' });

2.2 文件日志

const fs = require('fs');
const logStream = fs.createWriteStream('app.log', { flags: 'a' });

function log(message) {
  logStream.write(`${new Date().toISOString()} ${message}\n`);
}

三、日志分析

3.1 高频问题分析

# 提取用户问题
grep 'user_message' app.log | cut -d'"' -f4 | sort | uniq -c | sort -rn | head -20

3.2 错误统计

# 统计错误类型
grep 'ERROR' app.log | cut -d' ' -f5 | sort | uniq -c

3.3 性能分析

# 找出慢请求
awk '/response_time/ {print $NF}' app.log | awk '$1 > 1000'

四、可视化

4.1 ELK 集成

# filebeat.yml
filebeat.inputs:
  - type: log
    paths:
      - /var/log/ai-chat/*.log
    json.keys_under_root: true

output.elasticsearch:
  hosts: ["localhost:9200"]

4.2 Grafana 仪表盘

  • 对话量趋势
  • 错误率变化
  • 响应时间分布
  • 热门问题排行

五、告警规则

5.1 错误告警

alert: HighErrorRate
expr: rate(log_errors[5m]) > 0.05
for: 2m
annotations:
  summary: "错误率过高"

5.2 性能告警

alert: SlowResponse
expr: histogram_quantile(0.99, rate(response_time_bucket[5m])) > 2
for: 5m
annotations:
  summary: "P99 响应时间过高"

六、实战案例

案例:发现知识库缺口

步骤

  1. 分析未解决问题日志
  2. 提取高频失败问题
  3. 补充知识库
  4. 验证效果

效果

  • 解决率从 80% 提升到 88%

项目地址:GitHub - dingtalk-connector-pro 有问题欢迎 Issue 或评论区交流