[feature] Context API
還沒有人認領這個 Issue。
評估
研究方向
從現有的 Logger#with_level 行為以及提案中描述的 formatter 介面開始。確定每則訊息與巢狀作用域內容的 API 形式,包括與區塊記錄及現有自訂 formatter 的相容性。完成的標準是,實作議定的 Context API,不破壞目前的 formatter 使用方式,並涵蓋示範的範例。
由索引模型根據 Issue 內容生成。
描述
The logger gem is notoriously simple to use, but hard to extend.
One can only observe a few of the gems that added tags / json / logstash formatting support to see the same functionality reimplemented in "same but different" ways. For example, logstash-logger and the activesupport logger handle tagging in virtually the same way. sidekiq has a (IMO) convoluted API to add per-job context to log message, which somewhat works as long as one does not need per-fiber contexts. Several other "log as JSON" gems fight with the same issues to deliver something that works, and either heavily patch the Logger or Logger::Formatter or circumvent its usage altogether.
At a company I worked for, an internal logging library, built on top of logger, even added support for context by allowing functions like #info to receive a hash (i.e. log.info(message: "foo", user_id: 2, ...)), something which was completely non-standard and couldn't possibly work with the log.info { "foo" } idiom.
I think that some of these patterns could be coalesced into the main logger gem in a way that extending it should be simpler than what it is right now.
For the record, the default formatter already supports per-message context, but that context is limited to a few "static" parameters (severity, current time, process id, progname) which are logged in a non-standard manner:
Logger.new(STDOUT).info("foo")
#=> I, [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
There is currently no way to add more parameters, or (if necessary) override the existing ones.
Context API
This is a proposal to extend the Logger API in a way that both per-scope or per-message context can be passed downstream to the formatter(s), and logged accordingly:
logger = Logger.new(STDOUT)
logger.info("foo") #=> I, [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
logger.info { "foo" } #=> I, [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
# per message context
logger.info("foo", user_id: 1) #=> I, [user_id=1] [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
logger.info(user_id: 1) { "foo" } #=> I, [user_id=1] [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
# or
logger.info("foo", "user_id=1") #=> I, [user_id=1] [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
# per message context
logger.with_context(a: 1) do
logger.info("foo") #=> I, [a=1] [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
end
logger.with_context(a: 1) do
logger.with_context(b: 2) do
logger.info("foo") #=> I, [a=1] [b=2] [2025-08-13T15:00:03.830782 #5374] INFO -- : foo
end
end
This API would achieve the same goal as the #tagged examples liked above, or the sidekiq Logger#with function, in a standard manner, where formatters would just receive them as a bunch of key-value pairs (or an array of "string-able" objects, both work for me).
I think that it builds on top of the earlier addition of Logger#with_level, which does the scope-based change in a similar way, but didn't go further than providing this functionality for other context variables (this proposal fixes that).
By standardizing this part, adding i.e. a JSON or logstash formatter would be way less "involved", as one of the limitations nowadays is that there's no way to pass this per-message context via its API (this is the main reason why the sidekiq formatter uses thread variables).
Drawbacks
One of the main benefits of using the log.info { "msg" } idiom is that the "msg" string never gets allocated if the logger severity level skips logging the message. In the current proposal, the context is always present, and will result in a few allocations bubbling downstream. I'm not sure how important or negligible this concern is though.
A concern here would be backwards compatibility, as currently, custom formatters do not expose a method signature which allows sending kwargs. Perhaps there's a way to introspect the formatter method to infer whether the signature supports kwargs, but even if it does, the check does have its cost.
- 主要語言
- Ruby
- 星號
- 161
- 分支
- 76
- 平均合併
- 2 天 5 小時
- 30 天內合併 PR
- 3
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
ruby/logger 的其他 Issue
-
Programs using Logger sometimes resist SIGTERM / SIGINT可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
-
難度 3/5 1-2 天 新手友好度 68/100
-
難度 4/5 3-5 天 新手友好度 48/100
-
難度 3/5 1-2 天 新手友好度 52/100
-
`datetime_format=` does not apply to custom formatters可能已有人在做 @Rubyist007 於 229 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
SecureRandom.alphanumeric can silently return an empty or wrong-length secret instead of raising未關閉
難度 2/5 1-3 小時 新手友好度 67/100
ruby/securerandom#92 ·
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100
open-feature/ruby-sdk-contrib#340 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
zold-io/wts.zold.io#651 ·
維護者通常 1 天內回覆
-
good first issue help wanted
難度 2/5 1-3 小時 新手友好度 87/100
erkamyaman/rust-anything#788 ·
維護者通常 1 天內回覆