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