package logz import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" ) var ( // kratos映射zap的level levelMap = map[string]zapcore.Level{ "DEBUG": zapcore.DebugLevel, "INFO": zapcore.InfoLevel, "WARN": zapcore.WarnLevel, "ERROR": zapcore.ErrorLevel, "FATAL": zapcore.FatalLevel, } // 默认配置 defaultConfig = &Config{ Color: true, Level: "DEBUG", } zapStdLog *zapStd ) type ( zapStd struct { log *zap.SugaredLogger } Config struct { Color bool Level string } ) func NewLogger(conf *Config) *zapStd { if conf == nil { conf = defaultConfig } var level zapcore.Level if v, ok := levelMap[conf.Level]; ok { level = v } else { level = zapcore.InfoLevel } cfg := zap.NewProductionEncoderConfig() if conf.Color { // 颜色 cfg.EncodeLevel = zapcore.CapitalColorLevelEncoder } else { cfg.EncodeLevel = zapcore.CapitalLevelEncoder } //cfg.ConsoleSeparator = strings.Repeat(" ", 2) cfg.ConsoleSeparator = " ---- " // 指定日志时间格式 //cfg.EncodeTime = zapcore.ISO8601TimeEncoder cfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05") //cfg.EncodeCaller = zapcore.FullCallerEncoder // 使用控制台输出 encoder := zapcore.NewConsoleEncoder(cfg) core := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), level) //core := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(ws...), level) l := zap.New(zapcore.NewTee(core), zap.AddCaller(), zap.AddCallerSkip(1)) zapStdLog = &zapStd{ log: l.Sugar(), } return zapStdLog } func With(args ...interface{}) *zap.SugaredLogger { return zapStdLog.log.With(args...) } func Debug(args ...interface{}) { zapStdLog.log.Debug(args...) } func Info(args ...interface{}) { zapStdLog.log.Info(args...) } func Warn(args ...interface{}) { zapStdLog.log.Warn(args...) } func Error(args ...interface{}) { zapStdLog.log.Error(args...) } func Fatal(args ...interface{}) { zapStdLog.log.Fatal(args...) } func Debugf(format string, args ...interface{}) { zapStdLog.log.Debugf(format, args...) } func Infof(format string, args ...interface{}) { zapStdLog.log.Infof(format, args...) } func Warnf(format string, args ...interface{}) { zapStdLog.log.Warnf(format, args...) } func Errorf(format string, args ...interface{}) { zapStdLog.log.Errorf(format, args...) } func Fatalf(format string, args ...interface{}) { zapStdLog.log.Fatalf(format, args...) } func Println(args ...interface{}) { zapStdLog.log.Info(args...) } func Printf(format string, args ...interface{}) { zapStdLog.log.Infof(format, args...) } func Flush() { zapStdLog.log.Sync() }