日志排查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
插件实例的 loggerinstance、plugin

process 插件通过同一个包把 JSON 写到自己的 stderr(pkg/plugin/serve 负责设置,并带上调用的 run_id);Host 原样转发每条记录的事件名和 src,并补上实例。

3. agent 如何排查

  1. 用户报告失败。失败消息末尾带有 Run 编号,例如 抱歉,出错了:……(编号 run_2949...)。
  2. 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。
  3. 没有编号时:orquelo logs -c CONFIG -level warn -since 1h,或用 -event delivery. 只看某一领域,或用 -attr delivery_id=...。-json 输出原始行,便于 jq 处理。
  4. src 指向代码,事件名可直接搜索。进程的第一条 host.started 记录给出版本、commit、平台、配置修订号和各路径。
  5. 供应商失败带有供应商自己的编号,便于找其支持:模型 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
Runrun.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 Runtimeagent.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 可以 import log/slog;任何包都不能 import log。组件接收 *logging.Logger;nil logger 丢弃一切。
  • ◆每次调用都是 log.Info(ctx, "area.what", key, value, ...)(或 Debug、Warn、Error、Log):事件名是字符串字面量,在仓库内唯一;key 是字面量或词表中的 logging.Key* 常量。
  • ◆info 级别不记录密钥、消息正文或工具参数(第 5 节)。
  • ◆新的边界代码要记录结果:每次外部调用一条(带 dur_ms,失败时带 err),每次状态转换一条,每次拒绝一条并带稳定的 reason。