-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
42 lines (35 loc) · 878 Bytes
/
handler.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
package reqpretty
import (
"context"
"log/slog"
)
// LogHandler is a slog.Handler implementation that can be used to log to a file.
type LogHandler struct {
handler slog.Handler
}
// NewHandler creates a new LogHandler.
func NewHandler(logger *Logger) LogHandler {
if logger == nil {
logger = Default
}
return LogHandler{
handler: logger,
}
}
func (l LogHandler) Enabled(ctx context.Context, level slog.Level) bool {
return l.handler.Enabled(ctx, level)
}
func (l LogHandler) Handle(ctx context.Context, record slog.Record) error {
// Customize the logging format here
return l.handler.Handle(ctx, record)
}
func (l LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return LogHandler{
handler: l.handler.WithAttrs(attrs),
}
}
func (l LogHandler) WithGroup(name string) slog.Handler {
return LogHandler{
handler: l.handler.WithGroup(name),
}
}