-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhandler_inline_handle.go
64 lines (53 loc) · 1.64 KB
/
handler_inline_handle.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
package slogmulti
import (
"context"
"log/slog"
)
// NewHandleInlineHandler is a shortcut to a middleware that implements only the `Handle` method.
func NewHandleInlineHandler(handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error) slog.Handler {
return &HandleInlineHandler{
groups: []string{},
attrs: []slog.Attr{},
handleFunc: handleFunc,
}
}
var _ slog.Handler = (*HandleInlineHandler)(nil)
type HandleInlineHandler struct {
groups []string
attrs []slog.Attr
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error
}
// Implements slog.Handler
func (h *HandleInlineHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
// Implements slog.Handler
func (h *HandleInlineHandler) Handle(ctx context.Context, record slog.Record) error {
return h.handleFunc(ctx, h.groups, h.attrs, record)
}
// Implements slog.Handler
func (h *HandleInlineHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
newAttrs := []slog.Attr{}
newAttrs = append(newAttrs, h.attrs...)
newAttrs = append(newAttrs, attrs...)
return &HandleInlineHandler{
groups: h.groups,
attrs: newAttrs,
handleFunc: h.handleFunc,
}
}
// Implements slog.Handler
func (h *HandleInlineHandler) 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 &HandleInlineHandler{
groups: newGroups,
attrs: h.attrs,
handleFunc: h.handleFunc,
}
}