package wrapper import ( "encoding/json" "fmt" "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" "path" ) var Logger *zap.Logger func LoggerInit(logLevel string, logPath string, isDebug bool) { if _, err := os.Open(logPath); err != nil && os.IsNotExist(err) { p, _ := os.Getwd() logPath = path.Join(p, logPath) _, err = os.Create(logPath) } var js string if isDebug { js = fmt.Sprintf(`{ "level": "%s", "encoding": "json", "outputPaths": ["stdout","%s"], "errorOutputPaths": ["stdout"] }`, logLevel, logPath) } else { js = fmt.Sprintf(`{ "level": "%s", "encoding": "json", "outputPaths": ["%s"], "errorOutputPaths": ["%s"] }`, logLevel, logPath, logPath) } 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 { Logger.Error("init logger error: ", zap.String("err", err.Error())) } else { Logger.Info("log init") } } func InitLogger(logPath string) { }