-
Notifications
You must be signed in to change notification settings - Fork 55
/
cmtlog.go
96 lines (79 loc) · 2.21 KB
/
cmtlog.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
84
85
86
87
88
89
90
91
92
93
94
95
96
package app
import (
"context"
"strings"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/log"
cmtlog "github.com/cometbft/cometbft/libs/log"
)
var _ cmtlog.Logger = (*cmtLogger)(nil)
const (
levelError = iota + 1
levelInfo
levelDebug
)
// levels maps strings to numbers for easy comparison.
//
//nolint:gochecknoglobals // Global is ok here.
var levels = map[string]int{
"error": levelError,
"info": levelInfo,
"debug": levelDebug,
}
// dropCometDebugs is a map of cometBFT debug messages that should be dropped.
// These are super noisy and not useful.
//
//nolint:gochecknoglobals // Static mapping
var dropCometDebugs = map[string]bool{
"Read PacketMsg": true,
"Received bytes": true,
"Send": true,
"Receive": true,
"Sending vote message": true,
"Flush": true,
"setHasVote": true,
"TrySend": true,
}
// cmtLogger implements cmtlog.Logger by using the omni logging pattern.
// Comet log level is controlled separately in config.toml, since comet logs are very noisy.
type cmtLogger struct {
ctx context.Context //nolint:containedctx // This is a wrapper around the omni logger which is context based.
level int
}
func NewCmtLogger(ctx context.Context, levelStr string) (cmtlog.Logger, error) {
level, ok := levels[strings.ToLower(levelStr)]
if !ok {
return cmtLogger{}, errors.New("invalid comet log level", "level", levelStr)
}
return cmtLogger{
ctx: log.WithSkip(ctx, 4), // Skip this logger.
level: level,
}, nil
}
func (c cmtLogger) Debug(msg string, keyvals ...any) {
if c.level < levelDebug {
return
} else if dropCometDebugs[msg] {
return
}
log.Debug(c.ctx, msg, keyvals...)
}
func (c cmtLogger) Info(msg string, keyvals ...any) {
if c.level < levelInfo {
return
}
log.Info(c.ctx, msg, keyvals...)
}
func (c cmtLogger) Error(msg string, keyvals ...any) {
if c.level < levelError {
return
}
keyvals, err := splitOutError(keyvals)
log.Error(c.ctx, msg, err, keyvals...)
}
func (c cmtLogger) With(keyvals ...any) cmtlog.Logger { //nolint:ireturn // This signature is required by interface.
return cmtLogger{
ctx: log.WithCtx(c.ctx, keyvals...),
level: c.level,
}
}