-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhandler_inline.go
78 lines (66 loc) · 2.04 KB
/
handler_inline.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
package slogmulti
import (
"context"
"log/slog"
)
// NewInlineHandler is a shortcut to a handler that implements all methods.
func NewInlineHandler(
enabledFunc func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool,
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error,
) slog.Handler {
if enabledFunc == nil {
panic("slog-multi: enabledFunc is required")
}
if handleFunc == nil {
panic("slog-multi: handleFunc is required")
}
return &InlineHandler{
groups: []string{},
attrs: []slog.Attr{},
enabledFunc: enabledFunc,
handleFunc: handleFunc,
}
}
var _ slog.Handler = (*InlineHandler)(nil)
type InlineHandler struct {
groups []string
attrs []slog.Attr
enabledFunc func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error
}
// Implements slog.Handler
func (h *InlineHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.enabledFunc(ctx, h.groups, h.attrs, level)
}
// Implements slog.Handler
func (h *InlineHandler) Handle(ctx context.Context, record slog.Record) error {
return h.handleFunc(ctx, h.groups, h.attrs, record)
}
// Implements slog.Handler
func (h *InlineHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
newAttrs := []slog.Attr{}
newAttrs = append(newAttrs, h.attrs...)
newAttrs = append(newAttrs, attrs...)
return &InlineHandler{
groups: h.groups,
attrs: newAttrs,
enabledFunc: h.enabledFunc,
handleFunc: h.handleFunc,
}
}
// Implements slog.Handler
func (h *InlineHandler) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
newGroups := []string{}
newGroups = append(newGroups, h.groups...)
newGroups = append(newGroups, name)
return &InlineHandler{
groups: newGroups,
attrs: h.attrs,
enabledFunc: h.enabledFunc,
handleFunc: h.handleFunc,
}
}