-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
pipe.go
33 lines (27 loc) · 778 Bytes
/
pipe.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
package slogmulti
import (
"log/slog"
)
// Pipe defines a chain of Middleware.
type PipeBuilder struct {
middlewares []Middleware
}
// Pipe builds a chain of Middleware.
// Eg: rewrite log.Record on the fly for privacy reason.
func Pipe(middlewares ...Middleware) *PipeBuilder {
return &PipeBuilder{middlewares: middlewares}
}
// Implements slog.Handler
func (h *PipeBuilder) Pipe(middleware Middleware) *PipeBuilder {
h.middlewares = append(h.middlewares, middleware)
return h
}
// Implements slog.Handler
func (h *PipeBuilder) Handler(handler slog.Handler) slog.Handler {
for len(h.middlewares) > 0 {
middleware := h.middlewares[len(h.middlewares)-1]
h.middlewares = h.middlewares[0 : len(h.middlewares)-1]
handler = middleware(handler)
}
return handler
}