diff --git a/handler.go b/handler.go index ae48847..1c3b5de 100644 --- a/handler.go +++ b/handler.go @@ -53,6 +53,10 @@ func get(ctx context.Context) ctxVal { // Handler is a slog.Handler that adds context values to the log record. // Values are added via [WithValues]. +// +// Use one Handler in a chain of handlers, as the outermost handler. A Handler +// directly around another Handler adds the values once, but two Handlers +// separated by a different handler each add them. type Handler struct { h slog.Handler } @@ -64,10 +68,20 @@ func NewHandler(h slog.Handler) Handler { } func (h Handler) inner() slog.Handler { - if h.h == nil { - return slog.Default().Handler() + next := h.h + if next == nil { + next = slog.Default().Handler() } - return h.h + + // A nested Handler would add the context values to the record again. + switch nested := next.(type) { + case Handler: + return nested.inner() + case *Handler: + return nested.inner() + } + + return next } func (h Handler) Enabled(ctx context.Context, level slog.Level) bool { diff --git a/handler_test.go b/handler_test.go index 7c03be6..30174b3 100644 --- a/handler_test.go +++ b/handler_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "log" "log/slog" "reflect" "testing" @@ -51,3 +52,77 @@ func TestContextHandler(t *testing.T) { }) } } + +// A Handler around a Handler, as FromContext builds when the default handler +// is already a Handler, writes each context value once. +func TestContextHandlerWrappedTwice(t *testing.T) { + const once = `{"level":"INFO","msg":"","a":"b"}` + "\n" + + for _, tc := range []struct { + name string + log func(ctx context.Context, h slog.Handler) + want string + }{{ + name: "NewHandler of a Handler", + log: func(ctx context.Context, h slog.Handler) { + slog.New(NewHandler(NewHandler(h))).InfoContext(ctx, "") + }, + want: once, + }, { + name: "clog with a Handler as the default", + log: func(ctx context.Context, h slog.Handler) { + slog.SetDefault(slog.New(NewHandler(h))) + InfoContext(ctx, "") + }, + want: once, + }, { + name: "clog with a *Handler as the default", + log: func(ctx context.Context, h slog.Handler) { + hh := NewHandler(h) + slog.SetDefault(slog.New(&hh)) + InfoContext(ctx, "") + }, + want: once, + }, { + name: "slog with a Handler as the default", + log: func(ctx context.Context, h slog.Handler) { + slog.SetDefault(slog.New(NewHandler(h))) + slog.InfoContext(ctx, "") + }, + want: once, + }, { + name: "New(nil) with a Handler as the default", + log: func(ctx context.Context, h slog.Handler) { + slog.SetDefault(slog.New(NewHandler(h))) + New(nil).InfoContext(ctx, "") + }, + want: once, + }, { + name: "New(nil).With with a Handler as the default", + log: func(ctx context.Context, h slog.Handler) { + slog.SetDefault(slog.New(NewHandler(h))) + New(nil).With("x", "y").InfoContext(ctx, "") + }, + want: `{"level":"INFO","msg":"","x":"y","a":"b"}` + "\n", + }} { + t.Run(tc.name, func(t *testing.T) { + previous, output, flags := slog.Default(), log.Writer(), log.Flags() + t.Cleanup(func() { + slog.SetDefault(previous) + + // slog.SetDefault also sends the log package's output to the + // new handler, and restoring the previous default does not + // undo that. + log.SetOutput(output) + log.SetFlags(flags) + }) + + b := new(bytes.Buffer) + tc.log(WithValues(t.Context(), "a", "b"), slog.NewJSONHandler(b, testopts)) + + if got := b.String(); got != tc.want { + t.Errorf("got %q, want %q", got, tc.want) + } + }) + } +}