-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.go
54 lines (46 loc) · 1.1 KB
/
multi.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
package vlog
import (
"context"
"log/slog"
)
type MultiHandler struct {
handlers []slog.Handler
}
var _ slog.Handler = (*MultiHandler)(nil)
func (m *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool {
for _, h := range m.handlers {
if !h.Enabled(ctx, level) {
return false
}
}
return true
}
func (m *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
handlers := make([]slog.Handler, len(m.handlers))
for i, h := range m.handlers {
handlers[i] = h.WithAttrs(attrs)
}
return &MultiHandler{handlers: handlers}
}
func (m *MultiHandler) WithGroup(name string) slog.Handler {
handlers := make([]slog.Handler, len(m.handlers))
for i, h := range m.handlers {
handlers[i] = h.WithGroup(name)
}
return &MultiHandler{handlers: handlers}
}
func (m *MultiHandler) Handle(ctx context.Context, r slog.Record) error {
for _, handler := range m.handlers {
if !handler.Enabled(ctx, r.Level) {
continue
}
err := handler.Handle(ctx, r)
if err != nil {
return err
}
}
return nil
}
func NewMultiHandler(hs ...slog.Handler) slog.Handler {
return &MultiHandler{handlers: hs}
}