Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions slogr/slogr.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,24 @@ type SlogSink interface {
WithAttrs(attrs []slog.Attr) SlogSink
WithGroup(name string) SlogSink
}

// HandlerFromContext retrieves a slog.Handler from the context or, if no
// logger is stored there, returns the slog default handler.
//
// It uses logr.FromContext and thus is interoperable with code that only knows
// about the logr API.
func HandlerFromContext(ctx context.Context) slog.Handler {
logger, err := logr.FromContext(ctx)
if err == nil {
return NewSlogHandler(logger)
}
return slog.Default().Handler()
}

// ContextWithHandler creates a new context which contains the given handler.
//
// 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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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)?

return logr.NewContext(ctx, NewLogr(handler))
}
25 changes: 25 additions & 0 deletions slogr/slogr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,31 @@ func TestConversion(t *testing.T) {
}
}

func TestContext(t *testing.T) {
defaultLogger := slog.Default()
defer slog.SetDefault(defaultLogger)

// Retrieve default.
expectEqual(t, defaultLogger.Handler(), slogr.HandlerFromContext(context.Background()))
discard := slogr.NewSlogHandler(logr.Discard())

// Retrieve modified default.
slog.SetDefault(slog.New(discard))
expectEqual(t, discard, slogr.HandlerFromContext(context.Background()))

// Store and retrieve funcr.
funcrLogger := funcr.New(func(_, _ string) {}, funcr.Options{})
ctx := logr.NewContext(context.Background(), funcrLogger)
funcrHandler := slogr.HandlerFromContext(ctx)
expectEqual(t, funcrLogger, slogr.NewLogr(funcrHandler))

// Store and retrieve slog.JSONHandler.
jsonHandler := slog.NewJSONHandler(io.Discard, nil)
ctx = slogr.ContextWithHandler(context.Background(), jsonHandler)
jsonLogger := logr.FromContextOrDiscard(ctx)
expectEqual(t, jsonHandler, slogr.NewSlogHandler(jsonLogger))
}

func expectEqual(t *testing.T, expected, actual any) {
if expected != actual {
t.Helper()
Expand Down
Loading