-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
48 lines (41 loc) · 1.37 KB
/
handler.go
File metadata and controls
48 lines (41 loc) · 1.37 KB
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
package redact
import (
"context"
"log/slog"
)
// RedactionHandler is a custom slog.Handler that applies a redaction pipeline.
type RedactionHandler struct {
handler slog.Handler
pipeline *RedactionPipeline
}
// NewRedactionHandler initializes a new RedactionHandler.
func NewRedactionHandler(handler slog.Handler, pipeline *RedactionPipeline) *RedactionHandler {
return &RedactionHandler{
handler: handler,
pipeline: pipeline,
}
}
// Enabled reports whether the handler is enabled for the given level.
// Necessary to implement the slog.Handler interface.
func (h *RedactionHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.handler.Enabled(ctx, level)
}
// Handle processes the log record through the redaction pipeline before handling it
func (h *RedactionHandler) Handle(ctx context.Context, r slog.Record) error {
redactedRecord := h.pipeline.Process(ctx, r)
return h.handler.Handle(ctx, redactedRecord)
}
// WithAttrs returns a new handler with additional attributes.
func (h *RedactionHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &RedactionHandler{
handler: h.handler.WithAttrs(attrs),
pipeline: h.pipeline,
}
}
// WithGroup returns a new handler with the specified group.
func (h *RedactionHandler) WithGroup(name string) slog.Handler {
return &RedactionHandler{
handler: h.handler.WithGroup(name),
pipeline: h.pipeline,
}
}