-
Notifications
You must be signed in to change notification settings - Fork 53
/
sdklog.go
81 lines (64 loc) · 1.87 KB
/
sdklog.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
package app
import (
"context"
"github.com/omni-network/omni/lib/log"
sdklog "cosmossdk.io/log"
)
var _ sdklog.Logger = sdkLogger{}
// dropCosmosDebugs is a map of cosmosSDK debug messages that should be dropped.
// These are super noisy and not useful.
//
//nolint:gochecknoglobals // Static mapping
var dropCosmosDebugs = map[string]bool{
"recursiveRemove": true,
"BATCH SAVE": true,
"SAVE TREE": true,
}
// sdkLogger implements sdklog.Logger by using the omni logging pattern.
// Comet log level is controlled separately in config.toml, since comet logs are very noisy.
type sdkLogger struct {
ctx context.Context //nolint:containedctx // This is a wrapper around the omni logger which is context based.
}
func newSDKLogger(ctx context.Context) sdkLogger {
return sdkLogger{
ctx: log.WithSkip(ctx, 4), // Skip this logger.
}
}
func (c sdkLogger) Debug(msg string, keyvals ...any) {
if dropCosmosDebugs[msg] {
return
}
log.Debug(c.ctx, msg, keyvals...)
}
func (c sdkLogger) Info(msg string, keyvals ...any) {
log.Info(c.ctx, msg, keyvals...)
}
func (c sdkLogger) Warn(msg string, keyvals ...any) {
keyvals, err := splitOutError(keyvals)
log.Warn(c.ctx, msg, err, keyvals...)
}
func (c sdkLogger) Error(msg string, keyvals ...any) {
keyvals, err := splitOutError(keyvals)
log.Error(c.ctx, msg, err, keyvals...)
}
func (c sdkLogger) With(keyVals ...any) sdklog.Logger {
return sdkLogger{
ctx: log.WithCtx(c.ctx, keyVals...),
}
}
func (c sdkLogger) Impl() any {
return c
}
// splitOutError splits the keyvals into a slice of keyvals without the error and the error.
func splitOutError(keyvals []any) ([]any, error) {
var remaining []any
var err error
for i := 0; i < len(keyvals); i += 2 {
if keyErr, ok := keyvals[i+1].(error); ok {
err = keyErr
} else {
remaining = append(remaining, keyvals[i], keyvals[i+1])
}
}
return remaining, err
}