-
Notifications
You must be signed in to change notification settings - Fork 109
/
logging.go
114 lines (97 loc) · 3.09 KB
/
logging.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package common
import (
tmlog "github.com/tendermint/tendermint/libs/log"
"github.com/oasisprotocol/oasis-core/go/common/logging"
)
type logAdapter struct {
*logging.Logger
baseLogger *logging.Logger
suppressDebug bool
keyVals []interface{}
}
func (a *logAdapter) With(keyvals ...interface{}) tmlog.Logger {
// Tendermint uses `module` like oasis-node does, and to add insult to
// injury will cave off child loggers with subsequence calls to
// `With()`, resulting in multiple `module` keys.
//
// Do the right thing by:
// * Prefixing the `module` values with `tendermint:`
// * Coallece the multiple `module` values.
//
// This is more convoluted than it needs to be because the kit-log
// prefix vector is private.
findModule := func(vec []interface{}) (string, int) {
for i, v := range vec {
if i&1 != 0 {
continue
}
k := v.(string)
if k != "module" {
continue
}
if i+1 > len(vec) {
panic("With(): tendermint core logger, missing 'module' value")
}
vv := vec[i+1].(string)
return vv, i + 1
}
return "", -1
}
parentMod, parentIdx := findModule(a.keyVals)
childKeyVals := append([]interface{}{}, a.keyVals...)
childMod, childIdx := findModule(keyvals)
if childIdx < 0 {
// "module" was not specified for this child, use the one belonging
// to the parent.
if parentIdx < 0 {
// This should *NEVER* happen, if it does, it means that tendermint
// called `With()` on the base logAdapter without setting a module.
panic("With(): tendermint core logger, no sensible parent 'module'")
}
childKeyVals = append(childKeyVals, keyvals...)
} else if parentIdx < 0 {
// No parent logger, this must be a child of the base logAdapter.
keyvals[childIdx] = "tendermint:" + childMod
childKeyVals = append(childKeyVals, keyvals...)
} else {
// Append the child's module to the parent's.
childKeyVals[parentIdx] = parentMod + "/" + childMod
for i, v := range keyvals {
// And omit the non-re=written key/value from the those passed to
// the kit-log logger.
if i != childIdx-1 && i != childIdx {
childKeyVals = append(childKeyVals, v)
}
}
}
return &logAdapter{
Logger: a.baseLogger.With(childKeyVals...),
baseLogger: a.baseLogger,
suppressDebug: a.suppressDebug,
keyVals: childKeyVals,
}
}
func (a *logAdapter) Info(msg string, keyvals ...interface{}) {
a.Logger.Info(msg, keyvals...)
}
func (a *logAdapter) Error(msg string, keyvals ...interface{}) {
a.Logger.Error(msg, keyvals...)
}
func (a *logAdapter) Debug(msg string, keyvals ...interface{}) {
if !a.suppressDebug {
a.Logger.Debug(msg, keyvals...)
}
}
// NewLogAdapter creates a new adapter that adapts our logger to Tendermint APIs.
func NewLogAdapter(suppressDebug bool) tmlog.Logger {
// Need an extra level of unwinding because the Debug wrapper
// exists.
//
// This might be able to be replaced with the per-module log
// level instead.
return &logAdapter{
Logger: logging.GetLoggerEx("tendermint:base", 1),
baseLogger: logging.GetLoggerEx("", 1), // Tendermint sets the module, repeatedly.
suppressDebug: suppressDebug,
}
}