From 54321f5a2719754be8ef62a86fe741cd0b24d3d6 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Fri, 23 Oct 2020 11:04:29 +0800 Subject: [PATCH] Fix the error that windows cannot create soft connection --- server/config.yaml | 2 +- server/core/zap.go | 20 ++------------------ server/utils/rotatelogs_unix.go | 26 ++++++++++++++++++++++++++ server/utils/rotatelogs_windows.go | 23 +++++++++++++++++++++++ 4 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 server/utils/rotatelogs_unix.go create mode 100644 server/utils/rotatelogs_windows.go diff --git a/server/config.yaml b/server/config.yaml index 29cf05d8..04ff2570 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -56,7 +56,7 @@ mysql: config: 'charset=utf8mb4&parseTime=True&loc=Local' db-name: 'qmPlus' username: 'root' - password: 'Aa@6447985' + password: 'gdkid,,..' max-idle-conns: 10 max-open-conns: 10 log-mode: false diff --git a/server/core/zap.go b/server/core/zap.go index 3925b74f..9519cdd2 100644 --- a/server/core/zap.go +++ b/server/core/zap.go @@ -4,15 +4,13 @@ import ( "fmt" "gin-vue-admin/global" "gin-vue-admin/utils" - zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" - "path" "time" ) -var level zapcore.Level +var level zapcore.Level func Zap() (logger *zap.Logger) { if ok, _ := utils.PathExists(global.GVA_CONFIG.Zap.Director); !ok { // 判断是否有Director文件夹 @@ -50,20 +48,6 @@ func Zap() (logger *zap.Logger) { return logger } -// getWriteSyncer zap logger中加入file-rotatelogs -func getWriteSyncer() (zapcore.WriteSyncer, error) { - fileWriter, err := zaprotatelogs.New( - path.Join(global.GVA_CONFIG.Zap.Director, "%Y-%m-%d.log"), - zaprotatelogs.WithLinkName(global.GVA_CONFIG.Zap.LinkName), - zaprotatelogs.WithMaxAge(7*24*time.Hour), - zaprotatelogs.WithRotationTime(24*time.Hour), - ) - if global.GVA_CONFIG.Zap.LogInConsole { - return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err - } - return zapcore.AddSync(fileWriter), err -} - // getEncoderConfig 获取zapcore.EncoderConfig func getEncoderConfig() (config zapcore.EncoderConfig) { config = zapcore.EncoderConfig{ @@ -104,7 +88,7 @@ func getEncoder() zapcore.Encoder { // getEncoderCore 获取Encoder的zapcore.Core func getEncoderCore() (core zapcore.Core) { - writer, err := getWriteSyncer() // 使用file-rotatelogs进行日志分割 + writer, err := utils.GetWriteSyncer() // 使用file-rotatelogs进行日志分割 if err != nil { fmt.Printf("Get Write Syncer Failed err:%v", err.Error()) return diff --git a/server/utils/rotatelogs_unix.go b/server/utils/rotatelogs_unix.go new file mode 100644 index 00000000..f2759d8f --- /dev/null +++ b/server/utils/rotatelogs_unix.go @@ -0,0 +1,26 @@ +// +build !windows + +package utils + +import ( + "gin-vue-admin/global" + zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" + "go.uber.org/zap/zapcore" + "os" + "path" + "time" +) + +// GetWriteSyncer zap logger中加入file-rotatelogs +func GetWriteSyncer() (zapcore.WriteSyncer, error) { + fileWriter, err := zaprotatelogs.New( + path.Join(global.GVA_CONFIG.Zap.Director, "%Y-%m-%d.log"), + zaprotatelogs.WithLinkName(global.GVA_CONFIG.Zap.LinkName), + zaprotatelogs.WithMaxAge(7*24*time.Hour), + zaprotatelogs.WithRotationTime(24*time.Hour), + ) + if global.GVA_CONFIG.Zap.LogInConsole { + return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err + } + return zapcore.AddSync(fileWriter), err +} \ No newline at end of file diff --git a/server/utils/rotatelogs_windows.go b/server/utils/rotatelogs_windows.go new file mode 100644 index 00000000..6c7a3742 --- /dev/null +++ b/server/utils/rotatelogs_windows.go @@ -0,0 +1,23 @@ +package utils + +import ( + "gin-vue-admin/global" + zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" + "go.uber.org/zap/zapcore" + "os" + "path" + "time" +) + +// GetWriteSyncer zap logger中加入file-rotatelogs +func GetWriteSyncer() (zapcore.WriteSyncer, error) { + fileWriter, err := zaprotatelogs.New( + path.Join(global.GVA_CONFIG.Zap.Director, "%Y-%m-%d.log"), + zaprotatelogs.WithMaxAge(7*24*time.Hour), + zaprotatelogs.WithRotationTime(24*time.Hour), + ) + if global.GVA_CONFIG.Zap.LogInConsole { + return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err + } + return zapcore.AddSync(fileWriter), err +} \ No newline at end of file