arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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	log = logger.Sugar()
 61
 62	if errorsPath != "" {
 63		log.Infof("file where errors will be written: %s", errorsPath)
 64		errorsFile, err = os.OpenFile(errorsPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
 65		if err != nil {
 66			panic(err)
 67		}
 68	}
 69
 70	log.Infof("log level: %s", level)
 71}
 72
 73func writeToErrorsFile(msg string) {
 74	if errorsFile == nil {
 75		return
 76	}
 77	//nolint:errcheck
 78	errorsFile.WriteString(fmt.Sprintf("%s %s\n", time.Now().Format(time.RFC3339), msg))
 79}
 80
 81// Debug calls log.Debug
 82func Debug(args ...interface{}) {
 83	log.Debug(args...)
 84}
 85
 86// Info calls log.Info
 87func Info(args ...interface{}) {
 88	log.Info(args...)
 89}
 90
 91// Warn calls log.Warn
 92func Warn(args ...interface{}) {
 93	log.Warn(args...)
 94}
 95
 96// Error calls log.Error and stores the error message into the ErrorFile
 97func Error(args ...interface{}) {
 98	log.Error(args...)
 99	go writeToErrorsFile(fmt.Sprint(args...))
100}
101
102// Debugf calls log.Debugf
103func Debugf(template string, args ...interface{}) {
104	log.Debugf(template, args...)
105}
106
107// Infof calls log.Infof
108func Infof(template string, args ...interface{}) {
109	log.Infof(template, args...)
110}
111
112// Warnf calls log.Warnf
113func Warnf(template string, args ...interface{}) {
114	log.Warnf(template, args...)
115}
116
117// Errorf calls log.Errorf and stores the error message into the ErrorFile
118func Errorf(template string, args ...interface{}) {
119	log.Errorf(template, args...)
120	go writeToErrorsFile(fmt.Sprintf(template, args...))
121}