forked from samber/slog-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_inline_handle.go
44 lines (35 loc) · 1.32 KB
/
middleware_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
package slogmulti
import (
"context"
"log/slog"
)
// NewHandleInlineMiddleware is a shortcut to a middleware that implements only the `Handle` method.
func NewHandleInlineMiddleware(handleFunc func(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error) Middleware {
return func(next slog.Handler) slog.Handler {
return &HandleInlineMiddleware{
next: next,
handleFunc: handleFunc,
}
}
}
var _ slog.Handler = (*HandleInlineMiddleware)(nil)
type HandleInlineMiddleware struct {
next slog.Handler
handleFunc func(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error
}
// Implements slog.Handler
func (h *HandleInlineMiddleware) Enabled(ctx context.Context, level slog.Level) bool {
return h.next.Enabled(ctx, level)
}
// Implements slog.Handler
func (h *HandleInlineMiddleware) Handle(ctx context.Context, record slog.Record) error {
return h.handleFunc(ctx, record, h.next.Handle)
}
// Implements slog.Handler
func (h *HandleInlineMiddleware) WithAttrs(attrs []slog.Attr) slog.Handler {
return NewHandleInlineMiddleware(h.handleFunc)(h.next.WithAttrs(attrs))
}
// Implements slog.Handler
func (h *HandleInlineMiddleware) WithGroup(name string) slog.Handler {
return NewHandleInlineMiddleware(h.handleFunc)(h.next.WithGroup(name))
}