Skip to content

Instantly share code, notes, and snippets.

@hmli
Created June 18, 2017 11:24
Show Gist options
  • Select an option

  • Save hmli/756b69b3cddc349269f2ff9b5c85d512 to your computer and use it in GitHub Desktop.

Select an option

Save hmli/756b69b3cddc349269f2ff9b5c85d512 to your computer and use it in GitHub Desktop.
Zap logger demo
package log
import (
"go.uber.org/zap"
"fmt"
"go.uber.org/zap/zapcore"
"time"
)
var Log *zap.SugaredLogger
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
}
func LogInit() {
dyn := zap.NewAtomicLevel()
dyn.SetLevel(zap.DebugLevel)
cfg := zap.Config{
Level: dyn,
Development: true,
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "logger",
CallerKey: "C",
MessageKey: "msg",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
},
OutputPaths: []string{"stdout", "./info.log"},
ErrorOutputPaths: []string{"stderr", "./error.log"},
}
zLog, err := cfg.Build()
if err != nil {
fmt.Println(err)
return
}
Log = zLog.Sugar()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment