zap 高性能日志

最后更新于:2022-04-02 02:38:31

[TOC] >[github](https://github.com/uber-go/zap) ## 使用
libs/log/interface.go ``` package logs type InterfaceLogs interface { Debug(args ...interface{}) Info(args ...interface{}) Warn(args ...interface{}) Error(args ...interface{}) Panic(args ...interface{}) Fatal(args ...interface{}) Debugf(template string, args ...interface{}) Infof(template string, args ...interface{}) Warnf(template string, args ...interface{}) Errorf(template string, args ...interface{}) Panicf(template string, args ...interface{}) Fatalf(template string, args ...interface{}) } ```

libs/logs/logs.go ``` package logs import ( "antbiz/configs" "errors" "os" "sync" "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var ( Logs InterfaceLogs _once sync.Once ) func init() { _once.Do(func() { level := configs.Config.GetStringDefault("default.runmode", "prod") logName := os.Args[0] Logs = NewLogs(level, "./log/"+logName +".log") }) } func Debug(args ...interface{}) { Logs.Debug(args...) } func Info(args ...interface{}) { Logs.Info(args...) } func Warn(args ...interface{}) { Logs.Warn(args...) } func Error(args ...interface{}) { Logs.Error(args...) } func Panic(args ...interface{}) { Logs.Panic(args...) } func Fatal(args ...interface{}) { Logs.Fatal(args...) } func Debugf(template string, args ...interface{}) { Logs.Debugf(template, args...) } func Infof(template string, args ...interface{}) { Logs.Infof(template, args...) } func Warnf(template string, args ...interface{}) { Logs.Warnf(template, args...) } func Errorf(template string, args ...interface{}) { Logs.Errorf(template, args...) } func Panicf(template string, args ...interface{}) { Logs.Panicf(template, args...) } func Fatalf(template string, args ...interface{}) { Logs.Fatalf(template, args...) } type logs struct { zap *zap.SugaredLogger level string } func (l *logs) Debug(args ...interface{}) { l.zap.Debug(args...) } func (l *logs) Info(args ...interface{}) { l.zap.Info(args...) } func (l *logs) Warn(args ...interface{}) { l.zap.Warn(args...) } func (l *logs) Error(args ...interface{}) { l.zap.Error(args...) } func (l *logs) Panic(args ...interface{}) { l.zap.Panic(args...) } func (l *logs) Fatal(args ...interface{}) { l.zap.Fatal(args...) } func (l *logs) Debugf(template string, args ...interface{}) { l.zap.Debugf(template, args...) } func (l *logs) Infof(template string, args ...interface{}) { l.zap.Infof(template, args...) } func (l *logs) Warnf(template string, args ...interface{}) { l.zap.Warnf(template, args...) } func (l *logs) Errorf(template string, args ...interface{}) { l.zap.Errorf(template, args...) } func (l *logs) Panicf(template string, args ...interface{}) { l.zap.Panicf(template, args...) } func (l *logs) Fatalf(template string, args ...interface{}) { l.zap.Fatalf(template, args...) } func NewLogs(level string, file string) *logs { l := &logs{} lumberJackLogger := &lumberjack.Logger{ Filename: file, MaxSize: 1, MaxBackups: 5, //生成日志总数 MaxAge: 30, // 最大时间 Compress: true, //备份日志是否压缩为 gz文件 ,压缩率达到 96% } core := zapcore.NewCore( getEncoder(), zapcore.NewMultiWriteSyncer( zapcore.AddSync(lumberJackLogger), zapcore.AddSync(os.Stdout)), string2level(level)) logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(2), //设置打印的Caller趁机 ) l.zap = logger.Sugar() return l } func string2level(text string) zapcore.Level { switch text { case "dev": return zapcore.DebugLevel case "prod": return zapcore.InfoLevel default: panic(errors.New("log level is not found")) } } // 格式日志 func getEncoder() zapcore.Encoder { encoderConfig := zap.NewProductionEncoderConfig() encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder return zapcore.NewConsoleEncoder(encoderConfig) } ```

libs/logs/logs_test.go ``` package logs import ( "testing" ) func TestLogs(t *testing.T) { Debug("Debug") Debug("hello", "Debug") Warn("hello", "Warn") Info("hello", "Info") Error("hello", "Error") //Panic("Panic") //Fatal("Warn") Debugf("%v %v", "hello", "Debugf") Warnf("%v %v", "hello", "Warnf") Errorf("%v %v", "hello", "Errorf") //Panicf("%v%v", "hello", "Panicf") //Fatalf("%v%v", "hello", "Fatalf") } ```

';