forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmtlog.go
78 lines (63 loc) · 1.68 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
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,
}
// 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: ctx,
level: level,
}, nil
}
func (c cmtLogger) Debug(msg string, keyvals ...any) {
if c.level < levelDebug {
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,
}
}