forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
320 lines (271 loc) · 8.33 KB
/
log.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package log
import (
"fmt"
"os"
"strings"
"sync/atomic"
"github.com/0xPolygonHermez/zkevm-node"
"github.com/hermeznetwork/tracerr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LogEnvironment represents the possible log environments.
type LogEnvironment string
const (
// EnvironmentProduction production log environment.
EnvironmentProduction = LogEnvironment("production")
// EnvironmentDevelopment development log environment.
EnvironmentDevelopment = LogEnvironment("development")
)
// Logger is a wrapper providing logging facilities.
type Logger struct {
x *zap.SugaredLogger
}
// root logger
var log atomic.Pointer[Logger]
func getDefaultLog() *Logger {
l := log.Load()
if l != nil {
return l
}
// default level: debug
zapLogger, _, err := NewLogger(Config{
Environment: EnvironmentDevelopment,
Level: "debug",
Outputs: []string{"stderr"},
})
if err != nil {
panic(err)
}
log.Store(&Logger{x: zapLogger})
return log.Load()
}
// Init the logger with defined level. outputs defines the outputs where the
// logs will be sent. By default outputs contains "stdout", which prints the
// logs at the output of the process. To add a log file as output, the path
// should be added at the outputs array. To avoid printing the logs but storing
// them on a file, can use []string{"pathtofile.log"}
func Init(cfg Config) {
zapLogger, _, err := NewLogger(cfg)
if err != nil {
panic(err)
}
log.Store(&Logger{x: zapLogger})
}
// NewLogger creates the logger with defined level. outputs defines the outputs where the
// logs will be sent. By default, outputs contains "stdout", which prints the
// logs at the output of the process. To add a log file as output, the path
// should be added at the outputs array. To avoid printing the logs but storing
// them on a file, can use []string{"pathtofile.log"}
func NewLogger(cfg Config) (*zap.SugaredLogger, *zap.AtomicLevel, error) {
var level zap.AtomicLevel
err := level.UnmarshalText([]byte(cfg.Level))
if err != nil {
return nil, nil, fmt.Errorf("error on setting log level: %s", err)
}
var zapCfg zap.Config
switch cfg.Environment {
case EnvironmentProduction:
zapCfg = zap.NewProductionConfig()
default:
zapCfg = zap.NewDevelopmentConfig()
zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
zapCfg.Level = level
zapCfg.OutputPaths = cfg.Outputs
zapCfg.InitialFields = map[string]interface{}{
"version": zkevm.Version,
"pid": os.Getpid(),
}
logger, err := zapCfg.Build()
if err != nil {
return nil, nil, err
}
defer logger.Sync() //nolint:gosec,errcheck
// skip 2 callers: one for our wrapper methods and one for the package functions
withOptions := logger.WithOptions(zap.AddCallerSkip(2)) //nolint:gomnd
return withOptions.Sugar(), &level, nil
}
// WithFields returns a new Logger (derived from the root one) with additional
// fields as per keyValuePairs. The root Logger instance is not affected.
func WithFields(keyValuePairs ...interface{}) *Logger {
l := getDefaultLog().WithFields(keyValuePairs...)
// since we are returning a new instance, remove one caller from the
// stack, because we'll be calling the retruned Logger methods
// directly, not the package functions.
x := l.x.WithOptions(zap.AddCallerSkip(-1))
l.x = x
return l
}
// WithFields returns a new Logger with additional fields as per keyValuePairs.
// The original Logger instance is not affected.
func (l *Logger) WithFields(keyValuePairs ...interface{}) *Logger {
return &Logger{
x: l.x.With(keyValuePairs...),
}
}
func sprintStackTrace(st []tracerr.Frame) string {
builder := strings.Builder{}
// Skip deepest frame because it belongs to the go runtime and we don't
// care about it.
if len(st) > 0 {
st = st[:len(st)-1]
}
for _, f := range st {
builder.WriteString(fmt.Sprintf("\n%s:%d %s()", f.Path, f.Line, f.Func))
}
builder.WriteString("\n")
return builder.String()
}
// appendStackTraceMaybeArgs will append the stacktrace to the args
func appendStackTraceMaybeArgs(args []interface{}) []interface{} {
for i := range args {
if err, ok := args[i].(error); ok {
err = tracerr.Wrap(err)
st := tracerr.StackTrace(err)
return append(args, sprintStackTrace(st))
}
}
return args
}
// Debug calls log.Debug
func (l *Logger) Debug(args ...interface{}) {
l.x.Debug(args...)
}
// Info calls log.Info
func (l *Logger) Info(args ...interface{}) {
l.x.Info(args...)
}
// Warn calls log.Warn
func (l *Logger) Warn(args ...interface{}) {
l.x.Warn(args...)
}
// Error calls log.Error
func (l *Logger) Error(args ...interface{}) {
l.x.Error(args...)
}
// Fatal calls log.Fatal
func (l *Logger) Fatal(args ...interface{}) {
l.x.Fatal(args...)
}
// Debugf calls log.Debugf
func (l *Logger) Debugf(template string, args ...interface{}) {
l.x.Debugf(template, args...)
}
// Infof calls log.Infof
func (l *Logger) Infof(template string, args ...interface{}) {
l.x.Infof(template, args...)
}
// Warnf calls log.Warnf
func (l *Logger) Warnf(template string, args ...interface{}) {
l.x.Warnf(template, args...)
}
// Fatalf calls log.Fatalf
func (l *Logger) Fatalf(template string, args ...interface{}) {
l.x.Fatalf(template, args...)
}
// Errorf calls log.Errorf and stores the error message into the ErrorFile
func (l *Logger) Errorf(template string, args ...interface{}) {
l.x.Errorf(template, args...)
}
// Debug calls log.Debug on the root Logger.
func Debug(args ...interface{}) {
getDefaultLog().Debug(args...)
}
// Info calls log.Info on the root Logger.
func Info(args ...interface{}) {
getDefaultLog().Info(args...)
}
// Warn calls log.Warn on the root Logger.
func Warn(args ...interface{}) {
getDefaultLog().Warn(args...)
}
// Error calls log.Error on the root Logger.
func Error(args ...interface{}) {
args = appendStackTraceMaybeArgs(args)
getDefaultLog().Error(args...)
}
// Fatal calls log.Fatal on the root Logger.
func Fatal(args ...interface{}) {
args = appendStackTraceMaybeArgs(args)
getDefaultLog().Fatal(args...)
}
// Debugf calls log.Debugf on the root Logger.
func Debugf(template string, args ...interface{}) {
getDefaultLog().Debugf(template, args...)
}
// Infof calls log.Infof on the root Logger.
func Infof(template string, args ...interface{}) {
getDefaultLog().Infof(template, args...)
}
// Warnf calls log.Warnf on the root Logger.
func Warnf(template string, args ...interface{}) {
getDefaultLog().Warnf(template, args...)
}
// Fatalf calls log.Fatalf on the root Logger.
func Fatalf(template string, args ...interface{}) {
args = appendStackTraceMaybeArgs(args)
getDefaultLog().Fatalf(template, args...)
}
// Errorf calls log.Errorf on the root logger and stores the error message into
// the ErrorFile.
func Errorf(template string, args ...interface{}) {
args = appendStackTraceMaybeArgs(args)
getDefaultLog().Errorf(template, args...)
}
// appendStackTraceMaybeKV will append the stacktrace to the KV
func appendStackTraceMaybeKV(msg string, kv []interface{}) string {
for i := range kv {
if i%2 == 0 {
continue
}
if err, ok := kv[i].(error); ok {
err = tracerr.Wrap(err)
st := tracerr.StackTrace(err)
return fmt.Sprintf("%v: %v%v\n", msg, err, sprintStackTrace(st))
}
}
return msg
}
// Debugw calls log.Debugw
func (l *Logger) Debugw(msg string, kv ...interface{}) {
l.x.Debugw(msg, kv...)
}
// Infow calls log.Infow
func (l *Logger) Infow(msg string, kv ...interface{}) {
l.x.Infow(msg, kv...)
}
// Warnw calls log.Warnw
func (l *Logger) Warnw(msg string, kv ...interface{}) {
l.x.Warnw(msg, kv...)
}
// Errorw calls log.Errorw
func (l *Logger) Errorw(msg string, kv ...interface{}) {
l.x.Errorw(msg, kv...)
}
// Fatalw calls log.Fatalw
func (l *Logger) Fatalw(msg string, kv ...interface{}) {
l.x.Fatalw(msg, kv...)
}
// Debugw calls log.Debugw on the root Logger.
func Debugw(msg string, kv ...interface{}) {
getDefaultLog().Debugw(msg, kv...)
}
// Infow calls log.Infow on the root Logger.
func Infow(msg string, kv ...interface{}) {
getDefaultLog().Infow(msg, kv...)
}
// Warnw calls log.Warnw on the root Logger.
func Warnw(msg string, kv ...interface{}) {
getDefaultLog().Warnw(msg, kv...)
}
// Errorw calls log.Errorw on the root Logger.
func Errorw(msg string, kv ...interface{}) {
msg = appendStackTraceMaybeKV(msg, kv)
getDefaultLog().Errorw(msg, kv...)
}
// Fatalw calls log.Fatalw on the root Logger.
func Fatalw(msg string, kv ...interface{}) {
msg = appendStackTraceMaybeKV(msg, kv)
getDefaultLog().Fatalw(msg, kv...)
}