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 -}