-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapters.go
65 lines (51 loc) · 1.67 KB
/
adapters.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
package logz
import "github.com/rs/zerolog"
type Adapter interface {
Error(msg string, keysAndValues ...interface{})
Info(msg string, keysAndValues ...interface{})
Debug(msg string, keysAndValues ...interface{})
Warn(msg string, keysAndValues ...interface{})
}
type AdapterNoop struct{}
func (AdapterNoop) Error(msg string, keysAndValues ...interface{}) {}
func (AdapterNoop) Info(msg string, keysAndValues ...interface{}) {}
func (AdapterNoop) Debug(msg string, keysAndValues ...interface{}) {}
func (AdapterNoop) Warn(msg string, keysAndValues ...interface{}) {}
var _ Adapter = AdapterNoop{}
// AdapterKV fit for msg, keyvalue interface, Ex: retryablehttp.
//
// myLogFormat := log.With().Str("log_source", "mycomponent").Logger()
// kvLogger := logz.AdapterKV{Log: myLogFormat}
// kvLogger.Error("this is message", "err", "failed x")
type AdapterKV struct {
Log zerolog.Logger
FrameCount int
Caller bool
}
var _ Adapter = AdapterKV{}
func (l AdapterKV) frameUp() zerolog.Logger {
count := 3
if l.FrameCount > 0 {
count = l.FrameCount
}
if l.Caller {
return l.Log.With().CallerWithSkipFrameCount(count).Logger()
}
return l.Log.With().Logger()
}
func (l AdapterKV) Error(msg string, keysAndValues ...interface{}) {
f := l.frameUp()
f.Error().Fields(keysAndValues).Msg(msg)
}
func (l AdapterKV) Info(msg string, keysAndValues ...interface{}) {
f := l.frameUp()
f.Info().Fields(keysAndValues).Msg(msg)
}
func (l AdapterKV) Debug(msg string, keysAndValues ...interface{}) {
f := l.frameUp()
f.Debug().Fields(keysAndValues).Msg(msg)
}
func (l AdapterKV) Warn(msg string, keysAndValues ...interface{}) {
f := l.frameUp()
f.Warn().Fields(keysAndValues).Msg(msg)
}