/
simple_handler.go
59 lines (50 loc) · 1.3 KB
/
simple_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
package simple
import (
"context"
"log/slog"
"slices"
"github.com/jba/slog/withsupport"
)
func Handler(handle func(slog.Record) error, opts slog.HandlerOptions) slog.Handler {
return &simpleHandler{opts, handle, nil}
}
type simpleHandler struct {
opts slog.HandlerOptions
handle func(slog.Record) error
goa *withsupport.GroupOrAttrs
}
func (h *simpleHandler) Enabled(ctx context.Context, l slog.Level) bool {
minLevel := slog.LevelInfo
if h.opts.Level != nil {
minLevel = h.opts.Level.Level()
}
return l >= minLevel
}
func (h *simpleHandler) WithGroup(name string) slog.Handler {
h2 := *h
h2.goa = h2.goa.WithGroup(name)
return &h2
}
func (h *simpleHandler) WithAttrs(as []slog.Attr) slog.Handler {
h2 := *h
h2.goa = h2.goa.WithAttrs(as)
return &h2
}
func (h *simpleHandler) Handle(ctx context.Context, r slog.Record) error {
r2 := slog.NewRecord(r.Time, r.Level, r.Message, r.PC)
var attrs []slog.Attr
r.Attrs(func(a slog.Attr) bool { attrs = append(attrs, a); return true })
for g := h.goa; g != nil; g = g.Next {
if g.Group != "" {
anys := make([]any, len(attrs))
for i, a := range attrs {
anys[i] = a
}
attrs = []slog.Attr{slog.Group(g.Group, anys...)}
} else {
attrs = append(slices.Clip(g.Attrs), attrs...)
}
}
r2.AddAttrs(attrs...)
return h.handle(r2)
}