-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: slogr: add context support #213
Conversation
The normal logr API for storing and retrieving a logger is used. This is necessary to ensure interoperability. The downside is that storing a slog.Handler and retrieving it again is more costly than storing and retrieving a logr.Logger, because additional allocations are needed.
It "feels" wrong that context handling favors storing and retrieving a logr.Logger, but I think that's okay: I can't think of a strong reason why developers who want to support contextual logging (i.e. those who would call But do we then need the new functions at all? The alternative is what I had in #196:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read #196 as "store whatever you want, and tell us what you want to read". It's attractive in the sense that it doesn't waste allocations but I am really unsure of how it will be used.
Do you have a stronger sense of the real need here?
// | ||
// It stores the handler after conversion to a logr.Logger with logr.NewContext | ||
// and thus is interoperable with code that only knows about the logr API. | ||
func ContextWithHandler(ctx context.Context, handler slog.Handler) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be NewContext
for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted Handler
in the name because a variant with *slog.Logger
as parameter might also be useful at some point.
If we used slogr.NewContext
, it would probably be better defined as taking *slog.Logger
as parameter for full consistency with logr.NewContext
.
As it stands now, the name is more consistent with context.WithDeadline
or context.WithCancel
. We cannot have consistency with both the logr
and context
package, though, and consistency with logr
is more important ("closer" package).
NewContextWithHandler
(and future NewContextWithLogger
)?
Correct.
Not really. My initial goal was to support both APIs equally well, including context support, and then simply let developers pick what they prefer. Perhaps some developer has heard about slog, starting using it, and then wants to use it with contextual logging in APIs which already take a I've no idea how realistic that scenario is. One possible path forward might be to merge this API to show that this works (developers can always do something like this in their own code, using existing APIs - we just add some helpers) and if it gets used, improve the implementation while keeping the API the same: such an improvement would have to move most of the implementation into |
This assumes that context helper functions (go-logr#213) for slog will not get merged. If that is the consensus, then this documentation should be the last missing piece for slog support in logr.
This assumes that context helper functions (go-logr#213) for slog will not get merged. If that is the consensus, then this documentation should be the last missing piece for slog support in logr.
This assumes that context helper functions (go-logr#213) for slog will not get merged. If that is the consensus, then this documentation should be the last missing piece for slog support in logr.
This assumes that context helper functions (go-logr#213) for slog will not get merged. If that is the consensus, then this documentation should be the last missing piece for slog support in logr.
This assumes that context helper functions (go-logr#213) for slog will not get merged. If that is the consensus, then this documentation should be the last missing piece for slog support in logr.
This assumes that context helper functions (#213) for slog will not get merged. If that is the consensus, then this documentation should be the last missing piece for slog support in logr.
Closing in favor of more basic discussion in #234. |
The normal logr API for storing and retrieving a logger is used. This is necessary to ensure interoperability.
The downside is that storing a slog.Handler and retrieving it again is more costly than storing and retrieving a logr.Logger, because additional allocations are needed.
The new API is purely syntactic sugar. The same can also be done outside of logr.