hermez-node/log/log.go
log/log.goRaw
1package log
2
3import (
4 "fmt"
5 "time"
6
7 "go.uber.org/zap"
8 "go.uber.org/zap/zapcore"
9)
10
11var log *zap.SugaredLogger
12
13func init() {
14 // default level: debug
15 Init("debug", "")
16}
17
18// Init the logger with defined level. errorsPath defines the file where to store the errors, if set to "" will not store errors.
19func Init(levelStr, logPath string) {
20 var level zap.AtomicLevel
21 err := level.UnmarshalText([]byte(levelStr))
22 if err != nil {
23 panic(fmt.Errorf("Error on setting log level: %s", err))
24 }
25 outputPaths := []string{"stdout"}
26 if logPath != "" {
27 log.Infof("log file: %s", logPath)
28 outputPaths = append(outputPaths, logPath)
29 }
30
31 cfg := zap.Config{
32 Level: level,
33 Encoding: "console",
34 OutputPaths: outputPaths,
35 ErrorOutputPaths: []string{"stderr"},
36 EncoderConfig: zapcore.EncoderConfig{
37 MessageKey: "message",
38
39 LevelKey: "level",
40 EncodeLevel: zapcore.CapitalColorLevelEncoder,
41
42 TimeKey: "timestamp",
43 EncodeTime: func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
44 encoder.AppendString(ts.Local().Format(time.RFC3339))
45 },
46 EncodeDuration: zapcore.SecondsDurationEncoder,
47
48 CallerKey: "caller",
49 EncodeCaller: zapcore.ShortCallerEncoder,
50
51 StacktraceKey: "stacktrace",
52 LineEnding: zapcore.DefaultLineEnding,
53 },
54 }
55
56 logger, err := cfg.Build()
57 if err != nil {
58 panic(err)
59 }
60 //nolint:errcheck
61 defer logger.Sync()
62 withOptions := logger.WithOptions(zap.AddCallerSkip(1))
63 log = withOptions.Sugar()
64
65 log.Infof("log level: %s", level)
66}
67
68// Debug calls log.Debug
69func Debug(args ...interface{}) {
70 log.Debug(args...)
71}
72
73// Info calls log.Info
74func Info(args ...interface{}) {
75 log.Info(args...)
76}
77
78// Warn calls log.Warn
79func Warn(args ...interface{}) {
80 log.Warn(args...)
81}
82
83// Error calls log.Error
84func Error(args ...interface{}) {
85 log.Error(args...)
86}
87
88// Fatal calls log.Fatal
89func Fatal(args ...interface{}) {
90 log.Fatal(args...)
91}
92
93// Debugf calls log.Debugf
94func Debugf(template string, args ...interface{}) {
95 log.Debugf(template, args...)
96}
97
98// Infof calls log.Infof
99func Infof(template string, args ...interface{}) {
100 log.Infof(template, args...)
101}
102
103// Warnf calls log.Warnf
104func Warnf(template string, args ...interface{}) {
105 log.Warnf(template, args...)
106}
107
108// Fatalf calls log.Warnf
109func Fatalf(template string, args ...interface{}) {
110 log.Fatalf(template, args...)
111}
112
113// Errorf calls log.Errorf and stores the error message into the ErrorFile
114func Errorf(template string, args ...interface{}) {
115 log.Errorf(template, args...)
116}
117
118// Debugw calls log.Debugw
119func Debugw(template string, kv ...interface{}) {
120 log.Debugw(template, kv...)
121}
122
123// Infow calls log.Infow
124func Infow(template string, kv ...interface{}) {
125 log.Infow(template, kv...)
126}
127
128// Warnw calls log.Warnw
129func Warnw(template string, kv ...interface{}) {
130 log.Warnw(template, kv...)
131}
132
133// Errorw calls log.Errorw
134func Errorw(template string, kv ...interface{}) {
135 log.Fatalw(template, kv...)
136}
137
138// Fatalw calls log.Fatalw
139func Fatalw(template string, kv ...interface{}) {
140 log.Fatalw(template, kv...)
141}