Repository navigation
Conversation
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
added a commit
that referenced
this pull request
Nov 29, 2026
Owner
|
已在 main 以 30d6c08 重新實作並驗證,本 PR 由該提交取代,將納入 v1.5.2。logging.file_path 支援熱更新與約 32 MiB 紀錄邊界輪替、一份備份;獨立錯誤日誌路徑避免 JSONHandler 重入死鎖。切換失敗保留原 sink,API 缺欄保留、明確空字串關閉,已提供 WebUI。感謝提案;關閉舊分支而不直接合併。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
摘要
把结构化日志(现在只到 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,只留一个备份,不暴露大小配置。logging.dump_request_bodies之后,文件里也会有对话内容。README、中文文档和字段注释都写了这一点。LogFile.Write/rotateLocked时请注意:错误上报必须在锁外(见上面的死锁说明)。验证
gofmt -l .无输出;go build ./...、go vet ./...、go test -count=1 ./...全绿(Ubuntu 24.04,只有internal/config有测试)。运行时(编好的二进制 + 本地临时配置):
logging.file_path启动后文件被创建,启动日志同时出现在 stdout 和文件里。file_path的PUT /api/config返回 200;GET、磁盘配置、后续日志写入都还保留这个路径。不包含测试文件,与仓库现状一致。
配套前端:配置中心与诊断页里对应的控件单独放在
pr/webui-config分支的前端 PR(本仓库 #10:https://github.com/Meepo268/opencode2api/pull/10),等这一系列后端合入后对上游提交,不在本 PR 范围内。