From a525d4b1a748ae11182429fa6ec476e3faa9b5b7 Mon Sep 17 00:00:00 2001 From: yaooort Date: Sat, 9 Oct 2021 19:44:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Revert=20"=E5=8F=91=E5=B8=83V2.4.5=20RC"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 4465450b481d23a1f9eaf343e82b89e52b8029c4. --- server/core/server.go | 2 +- web/src/core/gin-vue-admin.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/core/server.go b/server/core/server.go index df0aa994..7d51edee 100644 --- a/server/core/server.go +++ b/server/core/server.go @@ -38,7 +38,7 @@ func RunWindowsServer() { fmt.Printf(` 欢迎使用 github.com/flipped-aurora/gin-vue-admin/server - 当前版本:V2.4.5 RC + 当前版本:V2.4.5 beta.1 加群方式:微信号:shouzi_1994 QQ群:622360840 默认自动化文档地址:http://127.0.0.1%s/swagger/index.html 默认前端文件运行地址:http://127.0.0.1:8080 diff --git a/web/src/core/gin-vue-admin.js b/web/src/core/gin-vue-admin.js index ab572ca7..f7025209 100644 --- a/web/src/core/gin-vue-admin.js +++ b/web/src/core/gin-vue-admin.js @@ -10,7 +10,7 @@ export default { register(app) console.log(` 欢迎使用 Gin-Vue-Admin - 当前版本:V2.4.5 RC + 当前版本:V2.4.5 beta.1 加群方式:微信:shouzi_1994 QQ群:622360840 默认自动化文档地址:http://127.0.0.1:${import.meta.env.VITE_SERVER_PORT}/swagger/index.html 默认前端文件运行地址:http://127.0.0.1:${import.meta.env.VITE_CLI_PORT} From 2264fb375046336d5bc53189ea6c6f0a655bdb16 Mon Sep 17 00:00:00 2001 From: yaooort Date: Sat, 9 Oct 2021 19:58:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9zap=E5=88=86=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=E6=89=93=E5=8D=B0=E5=88=B0=E6=97=A5=E5=BF=97=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 1 - server/config/zap.go | 1 - server/core/zap.go | 54 +++++++++++++----------------- server/go.mod | 1 + server/utils/rotatelogs.go | 28 ++++++++++++++++ server/utils/rotatelogs_unix.go | 32 ------------------ server/utils/rotatelogs_windows.go | 28 ---------------- 7 files changed, 53 insertions(+), 92 deletions(-) create mode 100644 server/utils/rotatelogs.go delete mode 100644 server/utils/rotatelogs_unix.go delete mode 100644 server/utils/rotatelogs_windows.go diff --git a/server/config.yaml b/server/config.yaml index 18454d82..76d293eb 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -12,7 +12,6 @@ zap: format: 'console' prefix: '[github.com/flipped-aurora/gin-vue-admin/server]' director: 'log' - link-name: 'latest_log' show-line: true encode-level: 'LowercaseColorLevelEncoder' stacktrace-key: 'stacktrace' diff --git a/server/config/zap.go b/server/config/zap.go index f681ca83..e5a00916 100644 --- a/server/config/zap.go +++ b/server/config/zap.go @@ -5,7 +5,6 @@ type Zap struct { Format string `mapstructure:"format" json:"format" yaml:"format"` // 输出 Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 日志前缀 Director string `mapstructure:"director" json:"director" yaml:"director"` // 日志文件夹 - LinkName string `mapstructure:"link-name" json:"linkName" yaml:"link-name"` // 软链接名称 ShowLine bool `mapstructure:"show-line" json:"showLine" yaml:"showLine"` // 显示行 EncodeLevel string `mapstructure:"encode-level" json:"encodeLevel" yaml:"encode-level"` // 编码级 StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktraceKey" yaml:"stacktrace-key"` // 栈名 diff --git a/server/core/zap.go b/server/core/zap.go index a0f40fe7..1abec961 100644 --- a/server/core/zap.go +++ b/server/core/zap.go @@ -11,38 +11,36 @@ import ( "go.uber.org/zap/zapcore" ) -var level zapcore.Level - func Zap() (logger *zap.Logger) { if ok, _ := utils.PathExists(global.GVA_CONFIG.Zap.Director); !ok { // 判断是否有Director文件夹 fmt.Printf("create %v directory\n", global.GVA_CONFIG.Zap.Director) _ = os.Mkdir(global.GVA_CONFIG.Zap.Director, os.ModePerm) } + // 调试级别 + debugPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev == zap.DebugLevel + }) + // 日志级别 + infoPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev == zap.InfoLevel + }) + // 警告级别 + warnPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev == zap.WarnLevel + }) + // 错误级别 + errorPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev >= zap.ErrorLevel + }) - switch global.GVA_CONFIG.Zap.Level { // 初始化配置文件的Level - case "debug": - level = zap.DebugLevel - case "info": - level = zap.InfoLevel - case "warn": - level = zap.WarnLevel - case "error": - level = zap.ErrorLevel - case "dpanic": - level = zap.DPanicLevel - case "panic": - level = zap.PanicLevel - case "fatal": - level = zap.FatalLevel - default: - level = zap.InfoLevel + cores := [...]zapcore.Core{ + getEncoderCore(fmt.Sprintf("./%s/server_debug.log", global.GVA_CONFIG.Zap.Director), debugPriority), + getEncoderCore(fmt.Sprintf("./%s/server_info.log", global.GVA_CONFIG.Zap.Director), infoPriority), + getEncoderCore(fmt.Sprintf("./%s/server_warn.log", global.GVA_CONFIG.Zap.Director), warnPriority), + getEncoderCore(fmt.Sprintf("./%s/server_error.log", global.GVA_CONFIG.Zap.Director), errorPriority), } + logger = zap.New(zapcore.NewTee(cores[:]...), zap.AddCaller()) - if level == zap.DebugLevel || level == zap.ErrorLevel { - logger = zap.New(getEncoderCore(), zap.AddStacktrace(level)) - } else { - logger = zap.New(getEncoderCore()) - } if global.GVA_CONFIG.Zap.ShowLine { logger = logger.WithOptions(zap.AddCaller()) } @@ -88,12 +86,8 @@ func getEncoder() zapcore.Encoder { } // getEncoderCore 获取Encoder的zapcore.Core -func getEncoderCore() (core zapcore.Core) { - writer, err := utils.GetWriteSyncer() // 使用file-rotatelogs进行日志分割 - if err != nil { - fmt.Printf("Get Write Syncer Failed err:%v", err.Error()) - return - } +func getEncoderCore(fileName string, level zapcore.LevelEnabler) (core zapcore.Core) { + writer := utils.GetWriteSyncer(fileName) // 使用file-rotatelogs进行日志分割 return zapcore.NewCore(getEncoder(), writer, level) } diff --git a/server/go.mod b/server/go.mod index 0cfd9d05..3fd450a3 100644 --- a/server/go.mod +++ b/server/go.mod @@ -19,6 +19,7 @@ require ( github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible github.com/mojocn/base64Captcha v1.3.1 + github.com/natefinch/lumberjack v2.0.0+incompatible // indirect github.com/qiniu/api.v7/v7 v7.4.1 github.com/robfig/cron/v3 v3.0.1 github.com/satori/go.uuid v1.2.0 diff --git a/server/utils/rotatelogs.go b/server/utils/rotatelogs.go new file mode 100644 index 00000000..ad205205 --- /dev/null +++ b/server/utils/rotatelogs.go @@ -0,0 +1,28 @@ +package utils + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/natefinch/lumberjack" + "go.uber.org/zap/zapcore" + "os" +) + +//@author: [SliverHorn](https://github.com/SliverHorn) +//@function: GetWriteSyncer +//@description: zap logger中加入file-rotatelogs +//@return: zapcore.WriteSyncer, error + +func GetWriteSyncer(file string) zapcore.WriteSyncer { + lumberJackLogger := &lumberjack.Logger{ + Filename: file, //日志文件的位置 + MaxSize: 10, //在进行切割之前,日志文件的最大大小(以MB为单位) + MaxBackups: 200, //保留旧文件的最大个数 + MaxAge: 30, //保留旧文件的最大天数 + Compress: true, //是否压缩/归档旧文件 + } + + if global.GVA_CONFIG.Zap.LogInConsole { + return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberJackLogger)) + } + return zapcore.AddSync(lumberJackLogger) +} diff --git a/server/utils/rotatelogs_unix.go b/server/utils/rotatelogs_unix.go deleted file mode 100644 index 44199f7c..00000000 --- a/server/utils/rotatelogs_unix.go +++ /dev/null @@ -1,32 +0,0 @@ -//go:build !windows -// +build !windows - -package utils - -import ( - "os" - "path" - "time" - - "github.com/flipped-aurora/gin-vue-admin/server/global" - zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" - "go.uber.org/zap/zapcore" -) - -//@author: [SliverHorn](https://github.com/SliverHorn) -//@function: GetWriteSyncer -//@description: zap logger中加入file-rotatelogs -//@return: zapcore.WriteSyncer, error - -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 -} diff --git a/server/utils/rotatelogs_windows.go b/server/utils/rotatelogs_windows.go deleted file mode 100644 index ae61d733..00000000 --- a/server/utils/rotatelogs_windows.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "os" - "path" - "time" - - "github.com/flipped-aurora/gin-vue-admin/server/global" - zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" - "go.uber.org/zap/zapcore" -) - -//@author: [SliverHorn](https://github.com/SliverHorn) -//@function: GetWriteSyncer -//@description: zap logger中加入file-rotatelogs -//@return: zapcore.WriteSyncer, error - -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 -}