Skip to content

proposal: x/exp/slog: add ability to attach Logger to context #62194

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

Closed
muhlemmer opened this issue Aug 21, 2023 · 2 comments
Closed

proposal: x/exp/slog: add ability to attach Logger to context #62194

muhlemmer opened this issue Aug 21, 2023 · 2 comments

Comments

@muhlemmer
Copy link
Contributor

It would be helpful if we can attach a slog.Logger to a context.Context, so that added attributes can be shared down the call chain. Much like library zerolog does with functions Ctx and Logger.WithContext).

Now that slog is becoming the standard log interface, it makes sense to also define context attachment. This would allow package authors to look for a logger in a context and if available use when needed. Likewise, it would give the author of a main package all the power to configure the logger, or not.

Use cases

  • Request logger created in middleware with request ID and other metadata, passed down in the http.Request context.
  • The ability to send the logger into a library, like facilitated in pgx-zerolog.

Using the current implementation

slog does provide the ability to pass a context using the XXXContext() methods. With a custom handler one could extract data from the context. However, this creates the need of implementing a custom Handler and trying each context value one wants to log. IMO this will result in much boilerplate code.

@gopherbot gopherbot added this to the Proposal milestone Aug 21, 2023
@seankhliao
Copy link
Member

This was a highly contentious point in the slog proposal, with a final decision to leave it out: #56345 (comment)

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2023
@veqryn
Copy link
Contributor

veqryn commented Apr 9, 2024

@golang golang locked and limited conversation to collaborators Apr 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants