uber log 使用

uber log 用来打印日志打印性能极高,以下是初始化日志logger 的代码


import (
       "encoding/json"
       "fmt"
       "log"

       "go.uber.org/zap"
       "go.uber.org/zap/zapcore"
)

var Logger *zap.Logger

func InitLogger() {
       // 日志地址
       lp := Conf.Common.LogPath
       // 日志级别 DEBUG,ERROR, INFO
       lv := Conf.Common.LogLevel
       // 是否 DEBUG
       isDebug := true
       if Conf.Common.IsDebug != true {
              isDebug = false
       }
       initLogger(lp, lv, isDebug)
       log.SetFlags(log.Lmicroseconds | log.Lshortfile | log.LstdFlags)
}

func initLogger(lp string, lv string, isDebug bool) {
       var js string
       if isDebug {
              js = fmt.Sprintf(`{
              "level": "%s",
              "encoding": "json",
              "outputPaths": ["stdout"],
              "errorOutputPaths": ["stdout"]
              }`, lv)
       } else {
              js = fmt.Sprintf(`{
              "level": "%s",
              "encoding": "json",
              "outputPaths": ["%s"],
              "errorOutputPaths": ["%s"]
              }`, lv, lp, lp)
       }

       var cfg zap.Config
       if err := json.Unmarshal([]byte(js), &cfg); err != nil {
              panic(err)
       }
       cfg.EncoderConfig = zap.NewProductionEncoderConfig()
       cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
       var err error
       Logger, err = cfg.Build()
       if err != nil {
              log.Fatal("init logger error: ", err)
       }
}

你可能感兴趣的:(go)