Should Libraries Log or Propagate Errors?
摘要
作者原帖正文:一直认为应用程序应记录日志(并出错),库则直接返回错误;今日惊讶发现各种生态(包括 Go)倾向在库中记录日志,甚至使静默这些日志成为重要任务。作者对比过往 view of logs(离散事件或小规模个人事情写文件 errs),尤其惊奇 Go slog 设计把此考虑纳入其中,因为可轻松传播与 enrich 错误。问:为什么不直接 err(并通过加到日志上下文再重试处理)?或在返回包中传递额外细节?哪些工作流等让记录日志更可取?作者仅能想到改变日志级别并静默随机额外信息的能力。
荐读理由
Go slog 库选择日志而非只返回 enriched error,支持业务层动态切换日志级别,便于在生产环境过滤无关调试输出
原文
My assumption was always: Applications log (and err), libraries err. Today, I was shocked to learn that various ecosystems love logging things in libraries (such that silencing those logs is even an important task). My view of logs has been around wide discrete events OR (for small scale personal things) writing errs in a file. I was particularly surprised to learn this was an important consideration of making Go's slog, since you can easily propagate and enrich errors... Why not just err (and handle them by e.g. adding to the log context then retrying)? Or pass extra details in the return package? What workflows etc. make this desirable? All I've been able to think of is the ability to change the log level and silence random extra information.
这条对你有帮助吗?