-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
83 lines (69 loc) · 1.99 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package groupcache
import (
"github.com/mailgun/groupcache/v2"
"darvaza.org/slog"
)
var (
_ groupcache.Logger = (*Logger)(nil)
)
// Logger is a specific log context for groupcache
type Logger struct {
logger slog.Logger
}
// Printf logs a message under a previously set level and with previously set fields
func (gcl *Logger) Printf(format string, args ...any) {
gcl.logger.Printf(format, args...)
}
// Error creates a new logger context with level set to Error
func (gcl *Logger) Error() groupcache.Logger {
return &Logger{
logger: gcl.logger.Error(),
}
}
// Warn creates a new logger context with level set to Warning
func (gcl *Logger) Warn() groupcache.Logger {
return &Logger{
logger: gcl.logger.Warn(),
}
}
// Info creates a new logger context with level set to Info
func (gcl *Logger) Info() groupcache.Logger {
return &Logger{
logger: gcl.logger.Info(),
}
}
// Debug creates a new logger context with level set to Debug
func (gcl *Logger) Debug() groupcache.Logger {
return &Logger{
logger: gcl.logger.Debug(),
}
}
// ErrorField creates a new logger context with a new field containing an error
func (gcl *Logger) ErrorField(label string, err error) groupcache.Logger {
return &Logger{
logger: gcl.logger.WithField(label, err),
}
}
// StringField creates a new logger context with a new field containing a string value
func (gcl *Logger) StringField(label string, val string) groupcache.Logger {
return &Logger{
logger: gcl.logger.WithField(label, val),
}
}
// WithFields creates a new logger context with a set of new fields of arbitrary value
func (gcl *Logger) WithFields(fields map[string]any) groupcache.Logger {
return &Logger{
logger: gcl.logger.WithFields(fields),
}
}
// NewLogger creates a Logger for groupcache wrapping a given slog.Logger
func NewLogger(l slog.Logger) groupcache.Logger {
return &Logger{
logger: l,
}
}
// SetLogger sets groupcache to use a given slog.Logger
func SetLogger(l slog.Logger) {
gcl := NewLogger(l)
groupcache.SetLoggerFromLogger(gcl)
}