package log import ( "os" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) // Use outside `package log` // should be avoided. var post *zap.Logger // Initialises the logger func InitLogger() { var err error mode := os.Getenv("runtime") if mode == "" { mode = "prod" } var level zapcore.Level if mode == "debug" { level = zap.DebugLevel } else { level = zap.InfoLevel } config := zap.NewDevelopmentConfig() config.DisableStacktrace = true config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder config.Level = zap.NewAtomicLevelAt(level) post, err = config.Build(zap.AddCallerSkip(1)) if err != nil { panic("Failed to initialize logger: " + err.Error()) } defer post.Sync() Info("Logger initialised", zap.String("level", level.String()), zap.String("runtime", mode)) } // Logs message at info level func Info(msg string, fields ...zap.Field) { post.Info(msg, fields...) } // Logs message at debug level func Debug(msg string, fields ...zap.Field) { post.Debug(msg, fields...) } // Logs message at warn level func Warn(msg string, fields ...zap.Field) { post.Warn(msg, fields...) } // Logs message at error level func Error(msg string, fields ...zap.Field) { post.Error(msg, fields...) } // Logs message at fatal level then call os.exit(1) func Fatal(msg string, fields ...zap.Field) { post.Fatal(msg, fields...) } // Logs message at panic level the panics func Panic(msg string, fields ...zap.Field) { post.Panic(msg, fields...) } // Flushes log messages func Cleanup() { Info("Flushing log messages") post.Sync() }