Skip to content

Pass FromContext(...) context to slog handler#425

Open
inteon wants to merge 2 commits into
go-logr:masterfrom
inteon:slog_context
Open

Pass FromContext(...) context to slog handler#425
inteon wants to merge 2 commits into
go-logr:masterfrom
inteon:slog_context

Conversation

@inteon

@inteon inteon commented Mar 9, 2026

Copy link
Copy Markdown

Instead of passing context.Background(), we now pass the context passed to FromContext(...) to the slog .Handle(...) function instead.

This is an alternative solution to the problem presented in #158 (only solves the use case where an slog backend is used).

cc @pohly

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>

@pohly pohly left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this only solves #158 for logging with a slog backend.

Comment thread slogr.go Outdated
Comment thread slogr.go Outdated
…thContext

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
@inteon inteon requested a review from pohly March 9, 2026 13:55
Comment thread slogr.go
// slog.LevelInfo and V(4) becomes slog.LevelDebug.
//
// The provided context is passed to the slog.Handler when logging. This allows
// the slog.Handler to use the context. This should only be used for short-lived

@obarisk obarisk Mar 10, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the following a little more descriptive?

an slog which following the lifecycle of the context

not limit to the vague long lived v.s. short lived

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide a diff? I would like to understand exactly what you would like to change, as I think that the current comment is quite clear already.

@obarisk

obarisk commented Mar 11, 2026

Copy link
Copy Markdown

following the context of #158
when we need specific context related event, e.g. trace context

context of logger is not really helpful if we are going to take the information in context for log events

with current implementation, we have to create logger for those events.

Comment thread slogr.go
// The provided context is passed to the slog.Handler when logging. This allows
// the slog.Handler to use the context. This should only be used for short-lived
// Loggers that do not outlive the context. If you need a long-lived Logger, use
// FromSlogHandler instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// FromSlogHandler instead.
// FromSlogHandler instead or
// pass context.Background().

@pohly pohly left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how useful this is. It only provides a benefit in a very limited scenario (slog handler is used which supports reading values from a context, log calls must be rewritten to inject the context via the new FromSlogHandlerWithContext). Does using logr provide any value at all in this scenario?

What about logr.FromContext? Should that automatically use FromSlogHandlerWithContext? That could make a difference because then apps don't need to be updated. However, we then can easily end up with creating loggers which outlive their original context and log the wrong values from that context later on, so this wouldn't be reliable.

cc @thockin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants