better-logging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBetter Logging
更优日志记录
Turn important operations into durable structured records that answer "what happened?" with one query instead of a trail of log lines.
Treat the unit of instrumentation as an operation owned by the runtime, not as individual log statements sprinkled through the codebase.
将重要操作转换为持久化的结构化记录,只需一次查询就能回答“发生了什么?”,无需追踪一连串日志行。
将埋点的单元视为运行时所属的操作,而非分散在代码库中的单个日志语句。
Quick Start
快速开始
- Run the audit script first:
bash
python3 scripts/audit_queryable_outcomes.py \
--root . \
--format markdownRun the bundled from this skill directory. If needed, resolve the directory containing this and pass the script path explicitly.
scripts/audit_queryable_outcomes.pySKILL.md- Read exactly one runtime reference before changing code:
- Electron, tRPC, IPC, background desktop jobs:
references/runtime-patterns-electron.md - Node HTTP apps and workers:
references/runtime-patterns-node.md
- Electron, tRPC, IPC, background desktop jobs:
- Use to define the canonical event shape before writing middleware or wrappers.
references/event-schema.md - Instrument the top 3 user-critical operations first. Do not start with every route or command in the repo.
- 首先运行审计脚本:
bash
python3 scripts/audit_queryable_outcomes.py \
--root . \
--format markdown从本skill目录运行内置的脚本。如有需要,找到包含此的目录,并显式传入脚本路径。
scripts/audit_queryable_outcomes.pySKILL.md- 在修改代码前,务必阅读至少一份运行时参考文档:
- Electron、tRPC、IPC、桌面后台任务:
references/runtime-patterns-electron.md - Node HTTP应用与工作进程:
references/runtime-patterns-node.md
- Electron、tRPC、IPC、桌面后台任务:
- 在编写中间件或包装器前,使用定义标准事件格式。
references/event-schema.md - 首先为Top 3用户关键操作添加埋点。不要一开始就覆盖仓库中的所有路由或命令。
Workflow
工作流程
- Inventory operation boundaries. Transport boundaries usually matter most: HTTP handlers, tRPC procedures, IPC commands, queue consumers, cron jobs, startup flows. Treat low-level helper functions as enrichment points, not as the canonical operation.
- Choose the operation record shape.
Read . Lock required fields first: operation name, type, timestamps, duration, success, stable error code, correlation ID, version metadata.
references/event-schema.md - Add one wrapper at the boundary.
Start the record at entry.
Enrich while the operation runs.
Finalize and persist in so success and failure paths both emit an outcome.
finally - Add domain context that improves debugging. Include actor, resource, trigger, rollout flags, retry count, and version data when available. Omit fields that are only interesting for local debugging or that contain secrets.
- Persist or emit the result durably. Prefer a local table, operational event store, or queryable analytics sink. Do not rely on ephemeral console logs or in-memory buffers for critical outcomes.
- Verify with real questions.
Read . If the new shape cannot answer failure, latency, and regression questions quickly, the instrumentation is still too thin.
references/rollout-and-queries.md
- 梳理操作边界。 传输边界通常最为重要:HTTP处理器、tRPC过程、IPC命令、队列消费者、定时任务、启动流程。 将底层辅助函数视为信息补充点,而非标准操作的载体。
- 选择操作记录格式。
阅读。 先锁定必填字段:操作名称、类型、时间戳、时长、成功状态、稳定错误码、关联ID、版本元数据。
references/event-schema.md - 在边界处添加包装器。
在入口处初始化记录。
在操作运行过程中补充信息。
在块中完成记录并持久化,确保成功和失败路径都会输出结果。
finally - 添加有助于调试的领域上下文。 尽可能包含参与者、资源、触发器、发布标记、重试次数和版本数据。 省略仅对本地调试有用或包含敏感信息的字段。
- 持久化或可靠输出结果。 优先选择本地表、操作事件存储或可查询的分析接收器。 关键结果不要依赖临时控制台日志或内存缓冲区。
- 用实际问题验证。
阅读。 如果新格式无法快速回答故障、延迟和回归问题,说明埋点仍然不够完善。
references/rollout-and-queries.md
Runtime Selection
运行时选择
- Electron desktop app:
Read for tRPC mutations,
references/runtime-patterns-electron.md, startup actions, and background jobs.ipcMain.handle - Node server or worker:
Read for Express, Fastify, Hono-style handlers, queues, and cron jobs.
references/runtime-patterns-node.md
- Electron桌面应用:
针对tRPC变更、、启动操作和后台任务,请阅读
ipcMain.handle。references/runtime-patterns-electron.md - Node服务器或工作进程:
针对Express、Fastify、Hono风格的处理器、队列和定时任务,请阅读。
references/runtime-patterns-node.md
Guardrails
注意事项
- Prefer one durable outcome event per important operation over many scattered log lines.
- Keep stable and machine-queryable. Treat free-form messages as secondary.
error_code - Record durations and retry counts on every critical operation.
- Add correlation fields early. A missing ,
request_id, oroperation_idweakens every downstream query.session_id - Separate operational truth from product analytics. Mirroring is fine; substitution is not.
- Redact or hash secrets, tokens, raw prompts, user content, and full URLs when they are not strictly required.
- Do not emit giant blobs by default. Persist pointers or summarized context instead.
- 优先为每个重要操作生成一个持久化结果事件,而非分散的多条日志行。
- 保持稳定且可被机器查询。将自由格式消息视为次要内容。
error_code - 为每个关键操作记录时长和重试次数。
- 尽早添加关联字段。缺失、
request_id或operation_id会削弱所有下游查询的有效性。session_id - 区分操作事实与产品分析数据。可以镜像数据,但不能相互替代。
- 当非严格必需时,编辑或哈希处理敏感信息、令牌、原始提示、用户内容和完整URL。
- 默认不要输出大型数据块。改为持久化指针或汇总后的上下文信息。
Resources
资源
- Scan a repo for likely operation boundaries, existing instrumentation, correlation fields, and durable outcome signals.
scripts/audit_queryable_outcomes.py - Required and recommended fields, naming guidance, and redaction rules.
references/event-schema.md - Boundary choices and wrapper patterns for Electron, tRPC, IPC, startup flows, and background jobs.
references/runtime-patterns-electron.md - Boundary choices and wrapper patterns for HTTP apps, queue consumers, and cron jobs.
references/runtime-patterns-node.md - Retrofit plan, acceptance checklist, and example questions the new instrumentation must answer.
references/rollout-and-queries.md
- 扫描仓库,识别可能的操作边界、现有埋点、关联字段和持久化结果信号。
scripts/audit_queryable_outcomes.py - 必填和推荐字段、命名指南及编辑规则。
references/event-schema.md - Electron、tRPC、IPC、启动流程和后台任务的边界选择与包装器模式。
references/runtime-patterns-electron.md - HTTP应用、队列消费者和定时任务的边界选择与包装器模式。
references/runtime-patterns-node.md - 改造计划、验收清单,以及新埋点必须能回答的示例问题。
references/rollout-and-queries.md