Skip to content

feat(telemetry): write structured logs to a rotating file sink - #116

Closed
Meepo268 wants to merge 3 commits into
jasonxu114514:mainfrom
Meepo268:pr/log-file
Closed

Meepo268 wants to merge 3 commits into
jasonxu114514:mainfrom
Meepo268:pr/log-file

Conversation

@Meepo268

@Meepo268 Meepo268 commented Oct 17, 2026 •

Copy link
Copy Markdown

摘要

把结构化日志(现在只到 stdout 和内存环)再写到磁盘:新增配置项 logging.file_path,写同一个 JSON sink,超过 32 MB 轮转一次,保留一个备份。

顺带修掉一个会让写日志自己卡死自己的 bug,并保证"设置页保存"不会把这个路径清掉。

动机

出问题时现在只能看 stdout 或者内存里那 2000 条环形缓冲;一旦进程崩了、或者部署环境把 stdout 收走了,现场就没了。按需开一个文件 sink,能事后翻,也能挂给外部采集。

主要改动

sink(internal/telemetry/logfile.go、internal/telemetry/logging.go)

  • LogFile 实现 io.Writer:rotateLocked 在写之前检查大小,超过 logFileLimit(32 MB)就把当前文件改名为 <path>.1,覆盖旧备份,然后重开。
  • Apply(path) 幂等:同一条路径重复调用不重新开文件,空路径表示关闭;切换路径时先关旧的再开新的。
  • multiSink 把每条记录同时写给 stdout 和文件;文件写是同步的(不缓冲),代价是每条一次 syscall,换来的是崩溃不丢尾部。
  • 死锁修复:轮转失败(比如目录不可写)时不能在持锁状态下回调错误上报器,否则上报器再写日志就会自己去抢同一把锁。现在把上报放到锁外。

接入(cmd/opencode2api/main.go、internal/gateway/runtime.go)

  • main 创建 sink、把错误上报器挂到 logger 上(log_file_error),启动时应用配置里的路径;打开失败只是 log_file_open_failed 警告,服务照常起。
  • defer 顺序保证 runtime manager 先关、sink 后关,所以关停时的日志也能落盘。
  • NewRuntimeManager 接收 sink,并在 applyLocked 里和 telemetry.SetLogLevel 并排重新应用 logging.file_path,也就是每次保存配置都会跟着切换;打开失败同样是警告,不让保存失败。

管理端(internal/admin/server.go)

  • handlePutConfig 拿请求体重建整份配置,旧版 WebUI 的日志表单里没有 file_path,一保存就会把这个字段写成空、等于把文件 sink 关掉。现在空值表示"保持当前路径",只有配置文件里显式写 "" 才是关闭。

文档(README.md、docs/management.zh-CN.md、config.example.json、.gitignore)

  • 配置表加了 logging.file_path 一行,中文文档加了 ## 日志落盘(键、32 MB 轮转、以及 dump_request_bodies 打开时文件里也会有人类对话内容这个注意事项)。
  • .gitignore 忽略 *.log.*(轮转出来的备份)和 logs/。

评审注意

  • 新键只有一个:logging.file_path,字符串,默认 ""(关闭)。轮转阈值是常量 logFileLimit,只留一个备份,不暴露大小配置。
  • 这个 PR 只管日志:没有路由、协议、通道粘性、模型目录或 WebUI 改动。
  • 文件里是和 stdout 一样的、已脱敏的 JSON 行;但开了 logging.dump_request_bodies 之后,文件里也会有对话内容。README、中文文档和字段注释都写了这一点。
  • 改 LogFile.Write/rotateLocked 时请注意:错误上报必须在锁外(见上面的死锁说明)。
  • 本 PR 不提供 WebUI 开关,只能改配置文件;按 feat: per-model proxy pools, A→B fallback, hidden models, stream resilience #67 第 8 条,UI 等后端定稿再提。

验证

gofmt -l . 无输出;go build ./...、go vet ./...、go test -count=1 ./... 全绿(Ubuntu 24.04,只有 internal/config 有测试)。

运行时(编好的二进制 + 本地临时配置):

  • 配 logging.file_path 启动后文件被创建,启动日志同时出现在 stdout 和文件里。
  • 用 WebUI 形状、省略 file_path 的 PUT /api/config 返回 200;GET、磁盘配置、后续日志写入都还保留这个路径。

不包含测试文件,与仓库现状一致。

配套前端:配置中心与诊断页里对应的控件单独放在 pr/webui-config 分支的前端 PR(本仓库 #10:https://github.com/Meepo268/opencode2api/pull/10),等这一系列后端合入后对上游提交,不在本 PR 范围内。

Operators had no durable log history: stdout depends on whatever
supervises the process, and the WebUI log page only holds the in-memory
ring. `logging.file_path` now mirrors the same redacted JSON records to a
file, rotating to `<path>.1` past 32 MB with exactly one backup, so a
restart or a "what happened last night" question can still be answered.

The sink is applied at startup and again on every configuration save, so
enabling, retargeting or disabling it never needs a restart, and a path
that cannot be opened is reported as a warning instead of blocking the
rest of the configuration. Relative paths resolve beside config.json.
…lock

Write held the sink mutex while invoking the error reporter, and the
reporter logs through the same structured logger whose sink is this file.
A rotation failure therefore re-entered Write on a mutex already held by
the same goroutine and hung the process instead of warning about it.

Rotation now returns the failure to Write, which reports it after
releasing the lock. The reopen paths are folded back together in the
process, so "rename failed, keep appending" and "reopen failed, disable
file logging" no longer duplicate their handle handling.
PUT /api/config replaces the whole logging section and the WebUI form does not render file_path yet, so a console save silently disabled the file sink. The current path is kept when the update does not carry one; clearing it stays a configuration-file edit. README and the management doc now say so.
@jasonxu114514

Copy link
Copy Markdown
Owner

已在 main 以 30d6c08 重新實作並驗證,本 PR 由該提交取代,將納入 v1.5.2。logging.file_path 支援熱更新與約 32 MiB 紀錄邊界輪替、一份備份;獨立錯誤日誌路徑避免 JSONHandler 重入死鎖。切換失敗保留原 sink,API 缺欄保留、明確空字串關閉,已提供 WebUI。感謝提案;關閉舊分支而不直接合併。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants