-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
48 lines (42 loc) · 1.06 KB
/
logger.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
package reqpretty
import (
"log/slog"
"path/filepath"
)
// Default is the default *Logger for the default handler.
var Default = &Logger{}
// Configure configures the logger by the given options.
func Configure(logger *Logger) {
if logger == nil {
logger = Default
}
customHandler := NewHandler(logger)
slog.SetDefault(slog.New(customHandler))
}
type Logger struct {
slog.Handler
}
func (o *Logger) clone() *Logger {
return &Logger{
Handler: o.Handler.WithAttrs(nil), // Make sure to clone the attributes
}
}
// defaultReplaceAttrFunc is a function that can be used to replace the value of an attribute.
// remove time key and source prefix
var defaultReplaceAttrFunc = func(groups []string, a slog.Attr) slog.Attr {
// Remove time attributes from the log.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
// Remove source filepath prefix attributes from the log.
if a.Key == slog.SourceKey {
source, ok := a.Value.Any().(*slog.Source)
if !ok {
return slog.Attr{}
}
if source != nil {
source.File = filepath.Base(source.File)
}
}
return a
}