zap_std.go 2.55 KB
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
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...)
}

kzkzzzz's avatar
kzkzzzz committed
120 121
func Println(args ...interface{}) {
	zapStdLog.log.Info(args...)
kzkzzzz's avatar
kzkzzzz committed
122 123 124 125 126 127 128 129 130
}

func Printf(format string, args ...interface{}) {
	zapStdLog.log.Infof(format, args...)
}

func Flush() {
	zapStdLog.log.Sync()
}