Skip to content

Commit

Permalink
rename to ContextFieldExtractor and allow multiple extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
recht committed Dec 7, 2023
1 parent 2cb0fec commit 63a8de9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
20 changes: 10 additions & 10 deletions exp/zapslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ import (
"go.uber.org/zap/zapcore"
)

// ContextExtractor can be used to extract fields from a context.
type ContextExtractor func(ctx context.Context) []zapcore.Field
// ContextFieldExtractor can be used to extract a field from a context.
type ContextFieldExtractor func(ctx context.Context) []zapcore.Field

// Handler implements the slog.Handler by writing to a zap Core.
type Handler struct {
core zapcore.Core
name string // logger name
addCaller bool
addStackAt slog.Level
callerSkip int
contextExtractor ContextExtractor
core zapcore.Core
name string // logger name
addCaller bool
addStackAt slog.Level
callerSkip int
contextFieldExtractors ContextFieldExtractor
}

// NewHandler builds a [Handler] that writes to the supplied [zapcore.Core]
Expand Down Expand Up @@ -162,8 +162,8 @@ func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
}

var contextFields []zapcore.Field
if h.contextExtractor != nil {
contextFields = h.contextExtractor(ctx)
for _, extractor := range h.contextFieldExtractors {
contextFields = append(contextFields, extractor(ctx)...)
}

fields := make([]zapcore.Field, 0, record.NumAttrs()+len(contextFields))
Expand Down
4 changes: 2 additions & 2 deletions exp/zapslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ func TestAttrKinds(t *testing.T) {
entry.ContextMap())
}

func TestContextExtractor(t *testing.T) {
func TestContextFieldExtractor(t *testing.T) {
key := testContextKey("testkey")
fac, logs := observer.New(zapcore.DebugLevel)
ctx := context.WithValue(context.Background(), key, "testvalue")

sl := slog.New(NewHandler(fac, WithContextExtractor(func(ctx context.Context) []zapcore.Field {
sl := slog.New(NewHandler(fac, WithContextFieldExtractor(func(ctx context.Context) []zapcore.Field {
v := ctx.Value(key).(string)
return []zapcore.Field{zap.String("testkey", v)}
})))
Expand Down
6 changes: 3 additions & 3 deletions exp/zapslog/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func AddStacktraceAt(lvl slog.Level) Option {
})
}

// .WithContextExtractor configures the Logger to extract fields from the context,
func WithContextExtractor(extractor ContextExtractor) Option {
// .WithContextFieldExtractor configures the Logger to extract fields from the context,
func WithContextFieldExtractors(extractors ...ContextFieldExtractor) Option {
return optionFunc(func(log *Handler) {
log.contextExtractor = extractor
log.contextFieldExtractors = append(log.contextFieldExtractors, extractors...)
})
}

0 comments on commit 63a8de9

Please sign in to comment.