-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctxkey.go
62 lines (48 loc) · 1.42 KB
/
ctxkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// copied and modified from https://github.com/alextanhongpin/go-core-microservice/blob/088138cbd567824ddec3cb39909d640c1b38a04a/http/types/contextkey.go
package ctxkey
import (
"context"
"errors"
"log/slog"
)
var ErrKeyNotFound = errors.New("context key not found")
type Key[T any] struct{}
func (k Key[T]) WithValue(ctx context.Context, t T) context.Context {
return context.WithValue(ctx, k, t)
}
func (k Key[T]) Value(ctx context.Context) (T, error) {
t, ok := ctx.Value(k).(T)
if !ok {
// since the key is a struct{} we have no way to find out which key was not found
return t, ErrKeyNotFound
}
return t, nil
}
func (k Key[T]) AttachToLog(key string, next slog.Handler) slog.Handler {
return slogHandler[T]{
logKey: key,
ctxKey: k,
next: next,
}
}
type slogHandler[T any] struct {
logKey string
ctxKey Key[T]
next slog.Handler
}
func (h slogHandler[T]) Enabled(ctx context.Context, level slog.Level) bool {
return h.next.Enabled(ctx, level)
}
func (h slogHandler[T]) Handle(ctx context.Context, record slog.Record) error {
val, ok := ctx.Value(h.ctxKey).(T)
if ok {
record.AddAttrs(slog.Any(h.logKey, val))
}
return h.next.Handle(ctx, record)
}
func (h slogHandler[T]) WithAttrs(attrs []slog.Attr) slog.Handler {
return h.ctxKey.AttachToLog(h.logKey, h.next.WithAttrs(attrs))
}
func (h slogHandler[T]) WithGroup(name string) slog.Handler {
return h.ctxKey.AttachToLog(h.logKey, h.next.WithGroup(name))
}