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