关于 error-detective
一项用于排查日志和代码中错误的开发技能,帮助开发者发现重复模式、关联事件,并根据错误详情和堆栈跟踪追查可能原因。
主要功能
- 01搜索并统计日志错误
- 02检测重复模式和错误峰值
- 03关联多个日志中的错误
- 04解析堆栈跟踪
- 05按检查清单开展调查
适用场景
- 01排查生产环境的 API 错误
- 02查找近期日志中的错误峰值
- 03跨请求和系统追踪错误
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ htlin222/ dotfiles --skill error-detective安装后在 Claude Code 中输入/error-detective确认已加载。
详细介绍
错误检测
查找并分析日志和代码中的错误。
适用场景
- 调查生产环境错误
- 分析日志模式
- 查找错误根因
- 关联不同系统中的错误
日志分析
查找错误
# Recent errors
grep -i "error\|exception\|fatal" /var/log/app.log | tail -100
# Errors with context
grep -B 5 -A 10 "ERROR" /var/log/app.log
# Count by error type
grep -oE "Error: [^:]*" app.log | sort | uniq -c | sort -rn
# Errors in time range
awk '/2024-01-15 14:/ && /ERROR/' app.log
模式检测
# Find repeated errors
grep "ERROR" app.log | cut -d']' -f2 | sort | uniq -c | sort -rn | head -20
# Correlate request IDs
grep "req-12345" *.log | sort -t' ' -k1,2
# Find error spikes
grep "ERROR" app.log | cut -d' ' -f1-2 | uniq -c | sort -rn
堆栈跟踪分析
解析堆栈跟踪
import re
def parse_stack_trace(log_content: str) -> list[dict]:
pattern = r'(?P<exception>\w+Error|\w+Exception): (?P<message>.*?)\n(?P<trace>(?:\s+at .+\n)+)'
traces = []
for match in re.finditer(pattern, log_content):
traces.append({
'type': match.group('exception'),
'message': match.group('message'),
'trace': match.group('trace').strip().split('\n')
})
return traces
常见模式
| 模式 | 表示 | 操作 |
|---|---|---|
| NullPointer | 缺少空值检查 | 添加验证 |
| Timeout | 依赖项响应缓慢 | 添加超时和重试 |
| Connection refused | 服务已停机 | 检查运行状况并重试 |
| OOM | 内存泄漏 | 进行性能分析并增加限制 |
| Rate limit | 请求过多 | 添加退避机制并排队 |
调查清单
- 捕获 - 获取完整的错误消息和堆栈跟踪
- 时间戳 - 何时开始的?
- 频率 - 发生频率如何?是否在增加?
- 范围 - 影响所有用户还是特定用户?
- 变更 - 最近是否部署过?
- 依赖项 - 外部服务是否受到影响?
关联查询
-- Errors by endpoint
SELECT endpoint, count(*) as errors
FROM logs
WHERE level = 'ERROR' AND time > NOW() - INTERVAL '1 hour'
GROUP BY endpoint ORDER BY errors DESC;
-- Error rate over time
SELECT
date_trunc('minute', time) as minute,
count(*) filter (where level = 'ERROR') as errors,
count(*) as total
FROM logs
WHERE time > NOW() - INTERVAL '1 hour'
GROUP BY minute ORDER BY minute;
示例
输入:“查找 API 返回 500 错误的原因” **操作:**搜索状态码为 500 的日志,查找堆栈跟踪,确定根因
输入:“分析过去一小时的错误模式” **操作:**按类型汇总错误,查找突增情况,并与事件关联
评论
支持 Markdown