日志排查doc/{zh,en}/design/logging.md
日志
状态:2026-11-13。已实现并测试:pkg/logging、日志文件与 orquelo logs 命令、Host 与插件的埋点、process 插件日志转发,以及约定检查。未实现:把日志发送到远端存储、orquelo logs 的持续跟随模式、指标。运行轨迹与指标的提议见运行轨迹与运行质量。
状态:2026-11-13。已实现并测试:pkg/logging、日志文件与 orquelo logs 命令、Host 与插件的埋点、process 插件日志转发,以及约定检查。未实现:把日志发送到远端存储、orquelo logs 的持续跟随模式、指标。运行轨迹与指标的提议见运行轨迹与运行质量。
线上问题主要由 code agent 排查。agent 读文件、跑命令,而不是看仪表盘。因此日志的设计目标是:用一条命令回答三个问题——这个 Run 发生了什么、最近哪里出错、这条记录是哪一行代码写的。
1. 日志在哪里
| 去向 | 格式 | 用途 |
|---|---|---|
<log.dir>/orquelo.jsonl(默认 state_dir/logs) | JSON Lines,每行一条记录;轮转为 orquelo.1.jsonl、orquelo.2.jsonl…… | agent 与工具:orquelo logs、jq、grep |
orquelo serve 的 stderr | 简短文本 | 看终端的人 |
yaml
1log:
2 level: info # debug | info | warn | error;serve -debug 设为 debug
3 dir: logs # 相对于配置文件
4 max_mb: 20 # 达到该大小时轮转
5 keep: 5 # 保留的轮转文件数2. 一条记录的样子
json
1{"time":"2026-11-13T16:18:41.808-08:00","level":"INFO","msg":"model.called",
2 "src":"internal/host/engine/caller.go:62","pid":16470,
3 "run_id":"run_2949231e3d0282a25ad6","connection":"console","conversation_id":"console","runtime":"agent",
4 "model":"openai","instance":"agent","dur_ms":4699,"tokens_in":1238,"tokens_out":89,"finish":"tool_calls"}- ◆
msg是稳定的事件名,形如area.what,在代码库中唯一。在代码中搜索"model.called"就能找到写它的那一行;src直接给出文件和行号。 - ◆key 来自同一份词表(
pkg/logging/keys.go):run_id、connection、conversation_id、event_id、instance、plugin、tool、call_key、model、delivery_id、question_id、dur_ms、err、err_code等。搜索run_id=X即可找到关于 Run X 的所有记录。 - ◆错误记为
err(文本)加err_code(稳定的plugin.Error代码,如llm.rate_limited、feishu.230002)。成功时没有err,因此按err过滤只会得到失败。 - ◆关联来自 context。 Host 在 context 上绑定一次身份信息,其下记录的每一条都带上它们:
| 绑定位置 | key |
|---|---|
| 正在路由的入站事件 | connection、event_id、conversation_id |
| Run(开始、恢复、结束) | run_id、connection、conversation_id、runtime |
| 工具调用 | tool、call_key |
| 提问 | question_id |
| 投递 | delivery_id、run_id、connection、conversation_id |
| 插件实例的 logger | instance、plugin |
process 插件通过同一个包把 JSON 写到自己的 stderr(pkg/plugin/serve 负责设置,并带上调用的 run_id);Host 原样转发每条记录的事件名和 src,并补上实例。
3. agent 如何排查
- 用户报告失败。失败消息末尾带有 Run 编号,例如
抱歉,出错了:……(编号 run_2949...)。 orquelo logs -c CONFIG -run run_2949...打印该 Run 的时间线:run.created、run.started、每次model.called与tool.called及耗时、question.asked/question.settled、delivery.sent,以及带state、err、err_code的run.finished。- 没有编号时:
orquelo logs -c CONFIG -level warn -since 1h,或用-event delivery.只看某一领域,或用-attr delivery_id=...。-json输出原始行,便于jq处理。 src指向代码,事件名可直接搜索。进程的第一条host.started记录给出版本、commit、平台、配置修订号和各路径。- 供应商失败带有供应商自己的编号,便于找其支持:模型 API 的
request_id,飞书错误消息中的log_id。
4. 记录哪些事件
| 领域 | 事件 |
|---|---|
| Host 生命周期 | host.started、host.stopped、config.reloaded、control.handled |
| 插件 | plugin.unavailable、plugin.requirement_unavailable、process.started、process.exited、process.start_failed、process.stderr、sdk.output |
| 路由 | route.decided(action、trigger、user)、route.failed、run.created |
| Run | run.started、run.finished(失败时为 warn,带 dur_ms)、run.requeued、run.panicked |
| 模型与工具 | model.called、tool.called、tool.refused(reason:not_found、changed、call_chain、policy、reviewer、not_approved)、tool.outcome_unknown、tool.replayed(debug) |
| 人工交互 | question.asked、question.settled、approval.reviewer_decided |
| 投递 | delivery.sent、delivery.retry_scheduled、delivery.failed_permanently |
| Agent Runtime | agent.step(debug)、agent.step_limit、agent.completion_retry、agent.output_cutoff、agent.content_filtered |
| 供应商 | llm.response(debug)、llm.http_error、llm.network_error、llm.retry(所有使用 pkg/modelhttp 的模型插件;日志行的 plugin 指明是哪个)、modelfiles.uploaded、modelfiles.deleted、modelfiles.abandoned(error)、feishu.message_received、feishu.message_dropped |
5. 安全
- ◆key 名表示密钥的值(
*secret*、*password*、*api_key*、authorization、cookie、*access_token*、token)写为[redacted]。 - ◆字符串值在 4 KiB 处截断,并标注截掉了多少。
- ◆第三方库通过
logging.SDK记录日志,它会去掉 URL 的查询串(连接 URL 带有 ticket)。 - ◆info 级别不记录消息正文和工具参数,只记录 id 和计数;debug 可以记录更多。
6. 代码规则
这些规则写在 AGENTS.md 第 12 节,并由 pkg/logging 中的 TestLogConventions 检查,make check 会运行它:
- ◆只有
pkg/logging可以 importlog/slog;任何包都不能 importlog。组件接收*logging.Logger;nil logger 丢弃一切。 - ◆每次调用都是
log.Info(ctx, "area.what", key, value, ...)(或Debug、Warn、Error、Log):事件名是字符串字面量,在仓库内唯一;key 是字面量或词表中的logging.Key*常量。 - ◆info 级别不记录密钥、消息正文或工具参数(第 5 节)。
- ◆新的边界代码要记录结果:每次外部调用一条(带
dur_ms,失败时带err),每次状态转换一条,每次拒绝一条并带稳定的reason。