Skip to content
Open
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
20 changes: 17 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
75 changes: 75 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"log"
"log/slog"
"reflect"
"testing"
Expand Down Expand Up @@ -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)
}
})
}
}
Loading