Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chatterjeesunit
Last active August 10, 2020 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chatterjeesunit/336ecad5098acfe5cd388d3557dac914 to your computer and use it in GitHub Desktop.
Save chatterjeesunit/336ecad5098acfe5cd388d3557dac914 to your computer and use it in GitHub Desktop.
var sugarLogger *zap.SugaredLogger
func main() {
InitLogger()
defer sugarLogger.Sync()
SimpleHttpGet("www.google.com")
SimpleHttpGet("http://www.google.com")
}
func InitLogger() {
writerSyncer := getLogWriter()
encoder := getEncoder()
core := zapcore.NewCore(encoder, writerSyncer, zapcore.DebugLevel)
logger := zap.New(core, zap.AddCaller())
sugarLogger = logger.Sugar()
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
}
func getLogWriter() zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: "./test.log",
MaxSize: 10,
MaxBackups: 5,
MaxAge: 30,
Compress: false,
}
return zapcore.AddSync(lumberJackLogger)
}
func SimpleHttpGet(url string) {
sugarLogger.Debugf("Trying to hit GET request for %s", url)
resp, err := http.Get(url)
if err != nil {
sugarLogger.Errorf("Error fetching URL %s : Error = %s", url, err)
} else {
sugarLogger.Infof("Success! statusCode = %s for URL %s", resp.Status, url)
resp.Body.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment