-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmiddleware_inline_with_group.go
56 lines (45 loc) · 1.56 KB
/
middleware_inline_with_group.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
package slogmulti
import (
"context"
"log/slog"
)
// NewWithGroupInlineMiddleware is a shortcut to a middleware that implements only the `WithAttrs` method.
func NewWithGroupInlineMiddleware(withGroupFunc func(name string, next func(string) slog.Handler) slog.Handler) Middleware {
return func(next slog.Handler) slog.Handler {
if next == nil {
panic("slog-multi: next is required")
}
if withGroupFunc == nil {
panic("slog-multi: withGroupFunc is required")
}
return &WithGroupInlineMiddleware{
next: next,
withGroupFunc: withGroupFunc,
}
}
}
var _ slog.Handler = (*WithGroupInlineMiddleware)(nil)
type WithGroupInlineMiddleware struct {
next slog.Handler
withGroupFunc func(name string, next func(string) slog.Handler) slog.Handler
}
// Implements slog.Handler
func (h *WithGroupInlineMiddleware) Enabled(ctx context.Context, level slog.Level) bool {
return h.next.Enabled(ctx, level)
}
// Implements slog.Handler
func (h *WithGroupInlineMiddleware) Handle(ctx context.Context, record slog.Record) error {
return h.next.Handle(ctx, record)
}
// Implements slog.Handler
func (h *WithGroupInlineMiddleware) WithAttrs(attrs []slog.Attr) slog.Handler {
return NewWithGroupInlineMiddleware(h.withGroupFunc)(h.next.WithAttrs(attrs))
}
// Implements slog.Handler
func (h *WithGroupInlineMiddleware) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
return NewWithGroupInlineMiddleware(h.withGroupFunc)(h.withGroupFunc(name, h.next.WithGroup))
}