Skip to content

Commit

Permalink
feat: adding context helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 21, 2024
1 parent 907c6fd commit bc36d76
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package slogcommon

import (
"context"
"log/slog"
)

func ContextExtractor(ctx context.Context, fns []func(ctx context.Context) []slog.Attr) []slog.Attr {
attrs := []slog.Attr{}
for _, fn := range fns {
attrs = append(attrs, fn(ctx)...)
}
return attrs
}

func ExtractFromContext(keys ...any) func(ctx context.Context) []slog.Attr {
return func(ctx context.Context) []slog.Attr {
attrs := []slog.Attr{}
for _, key := range keys {
attrs = append(attrs, slog.Any(key.(string), ctx.Value(key)))
}
return attrs
}
}

0 comments on commit bc36d76

Please sign in to comment.