Skip to content

Instantly share code, notes, and snippets.

@BNPrashanth
Created September 9, 2019 10:03
Show Gist options
  • Save BNPrashanth/99ba236ae0b750ef17f881e15be7ee34 to your computer and use it in GitHub Desktop.
Save BNPrashanth/99ba236ae0b750ef17f881e15be7ee34 to your computer and use it in GitHub Desktop.
package logger
import (
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
// Log variable is a globally accessible variable which will be initialized when the InitializeZapCustomLogger function is executed successfully.
Log *zap.Logger
)
/*
InitializeZapCustomLogger Funtion initializes a logger using uber-go/zap package in the application.
*/
func InitializeZapCustomLogger() {
conf := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
OutputPaths: []string{viper.GetString("logger-output-path"), "stdout"},
EncoderConfig: zapcore.EncoderConfig{
LevelKey: "level",
TimeKey: "time",
CallerKey: "file",
MessageKey: "msg",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
Log, _ = conf.Build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment