arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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 and stores the error message into the ErrorFile
 84func Error(args ...interface{}) {
 85	log.Error(args...)
 86}
 87
 88// Debugf calls log.Debugf
 89func Debugf(template string, args ...interface{}) {
 90	log.Debugf(template, args...)
 91}
 92
 93// Infof calls log.Infof
 94func Infof(template string, args ...interface{}) {
 95	log.Infof(template, args...)
 96}
 97
 98// Warnf calls log.Warnf
 99func Warnf(template string, args ...interface{}) {
100	log.Warnf(template, args...)
101}
102
103// Errorf calls log.Errorf and stores the error message into the ErrorFile
104func Errorf(template string, args ...interface{}) {
105	log.Errorf(template, args...)
106}
107
108// Debugw calls log.Debugw
109func Debugw(template string, kv ...interface{}) {
110	log.Debugw(template, kv...)
111}
112
113// Infow calls log.Infow
114func Infow(template string, kv ...interface{}) {
115	log.Infow(template, kv...)
116}
117
118// Warnw calls log.Warnw
119func Warnw(template string, kv ...interface{}) {
120	log.Warnw(template, kv...)
121}
122
123// Errorw calls log.Errorw and stores the error message into the ErrorFile
124func Errorw(template string, kv ...interface{}) {
125	log.Errorw(template, kv...)
126}