Created
June 18, 2017 11:24
-
-
Save hmli/756b69b3cddc349269f2ff9b5c85d512 to your computer and use it in GitHub Desktop.
Zap logger demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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