-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
90 lines (75 loc) · 1.84 KB
/
handler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package clog
import (
"context"
"log/slog"
)
var (
ctxKey = struct{}{}
)
type ctxVal map[string]any
// With returns a new context with the given values.
// Values are expected to be key-value pairs, where the key is a string.
// e.g. WithValues(ctx, "foo", "bar", "baz", 1)
// If a value already exists, it is overwritten.
// If an odd number of arguments are provided, With panics.
func WithValues(ctx context.Context, args ...any) context.Context {
if len(args)%2 != 0 {
panic("non-even number of arguments")
}
values := ctxVal{}
// Copy existing values
for k, v := range get(ctx) {
values[k] = v
}
for i := 0; i < len(args); i++ {
key, ok := args[i].(string)
if !ok {
panic("non-string key")
}
i++
if i >= len(args) {
break
}
value := args[i]
values[key] = value
}
return context.WithValue(ctx, ctxKey, values)
}
func get(ctx context.Context) ctxVal {
if value, ok := ctx.Value(ctxKey).(ctxVal); ok {
return value
}
return nil
}
// Handler is a slog.Handler that adds context values to the log record.
// Values are added via [WithValues].
type Handler struct {
h slog.Handler
}
// NewHandler configures a new context aware slog handler.
// If h is nil, the default slog handler is used.
func NewHandler(h slog.Handler) Handler {
return Handler{h}
}
func (h Handler) inner() slog.Handler {
if h.h == nil {
return slog.Default().Handler()
}
return h.h
}
func (h Handler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
func (h Handler) Handle(ctx context.Context, r slog.Record) error {
values := get(ctx)
for k, v := range values {
r.Add(k, v)
}
return h.inner().Handle(ctx, r)
}
func (h Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
return Handler{h.inner().WithAttrs(attrs)}
}
func (h Handler) WithGroup(name string) slog.Handler {
return Handler{h.inner().WithGroup(name)}
}