forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.go
136 lines (112 loc) · 3.13 KB
/
default.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package logging
import (
"fmt"
"os"
"github.com/gogo/protobuf/proto"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
tmctypes "github.com/tendermint/tendermint/rpc/coretypes"
"github.com/emrahm/juno/v5/modules"
"github.com/emrahm/juno/v5/types"
)
var (
_ Logger = &defaultLogger{}
)
// defaultLogger represents the default logger for any kind of error
type defaultLogger struct {
Logger zerolog.Logger
}
// DefaultLogger allows to build a new defaultLogger instance
func DefaultLogger() Logger {
return &defaultLogger{
Logger: log.Logger,
}
}
// SetLogLevel implements Logger
func (d *defaultLogger) SetLogLevel(level string) error {
logLvl, err := zerolog.ParseLevel(level)
if err != nil {
return err
}
zerolog.SetGlobalLevel(logLvl)
return nil
}
// SetLogFormat implements Logger
func (d *defaultLogger) SetLogFormat(format string) error {
switch format {
case "json":
// JSON is the default logging format
break
case "text":
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
default:
return fmt.Errorf("invalid logging format: %s", format)
}
return nil
}
// Info implements Logger
func (d *defaultLogger) Info(msg string, keyVals ...interface{}) {
d.Logger.Info().Fields(getLogFields(keyVals...)).Msg(msg)
}
// Debug implements Logger
func (d *defaultLogger) Debug(msg string, keyVals ...interface{}) {
d.Logger.Debug().Fields(getLogFields(keyVals...)).Msg(msg)
}
// Error implements Logger
func (d *defaultLogger) Error(msg string, keyVals ...interface{}) {
ErrorCount.Inc()
d.Logger.Error().Fields(getLogFields(keyVals...)).Msg(msg)
}
// GenesisError implements Logger
func (d *defaultLogger) GenesisError(module modules.Module, err error) {
d.Error("error while handling genesis",
"err", err,
LogKeyModule, module.Name(),
)
}
// BlockError implements Logger
func (d *defaultLogger) BlockError(module modules.Module, block *tmctypes.ResultBlock, err error) {
d.Error("error while handling block",
"err", err,
LogKeyModule, module.Name(),
LogKeyHeight, block.Block.Height,
)
}
// EventsError implements Logger
func (d *defaultLogger) EventsError(module modules.Module, block *tmctypes.ResultBlock, err error) {
d.Error("error while handling block events",
"err", err,
LogKeyModule, module.Name(),
LogKeyHeight, block.Block.Height,
)
}
// TxError implements Logger
func (d *defaultLogger) TxError(module modules.Module, tx *types.Tx, err error) {
d.Error("error while handling transaction",
"err", err,
LogKeyModule, module.Name(),
LogKeyHeight, tx.Height,
LogKeyTxHash, tx.TxHash,
)
}
// MsgError implements Logger
func (d *defaultLogger) MsgError(module modules.Module, tx *types.Tx, msg sdk.Msg, err error) {
d.Error("error while handling message",
"err", err,
LogKeyModule, module.Name(),
LogKeyHeight, tx.Height,
LogKeyTxHash, tx.TxHash,
LogKeyMsgType, proto.MessageName(msg),
)
}
func getLogFields(keyVals ...interface{}) map[string]interface{} {
if len(keyVals)%2 != 0 {
return nil
}
fields := make(map[string]interface{})
for i := 0; i < len(keyVals); i += 2 {
fields[keyVals[i].(string)] = keyVals[i+1]
}
return fields
}