From ac8a04fbd9b1166a8e55b2afd026710d7b9185af Mon Sep 17 00:00:00 2001 From: zhangxin Date: Sun, 18 Apr 2021 17:20:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E5=8F=A5?= =?UTF-8?q?=E6=9F=84=E4=BD=BF=E7=94=A8=E4=B8=80=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/logger/config.go | 6 +++--- infra/logger/config.yaml | 17 +++++++++++------ infra/logger/file.go | 21 ++++++++++++++++----- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/infra/logger/config.go b/infra/logger/config.go index a4aaa42..6589465 100644 --- a/infra/logger/config.go +++ b/infra/logger/config.go @@ -26,10 +26,10 @@ type db struct { type file struct { Filename string `yaml:"filename"` - MaxSize int `yaml:"maxSize"` + //MaxSize int `yaml:"maxSize"` MaxAge int `yaml:"maxAge"` - MaxBackups int `yaml:"maxBackups"` - Compress bool `yaml:"compress"` + //MaxBackups int `yaml:"maxBackups"` + //Compress bool `yaml:"compress"` } type console struct { diff --git a/infra/logger/config.yaml b/infra/logger/config.yaml index d0c5b98..da36d33 100644 --- a/infra/logger/config.yaml +++ b/infra/logger/config.yaml @@ -16,8 +16,11 @@ file: filename: ./log/common.log # 支持文件的最大个数 MaxAge: 30 -# 日志级别 - level: debug + user: + # 文件location + filename: ./log/user.log + # 支持文件的最大个数 + MaxAge: 7 # console 为输出到控制台 console: @@ -46,8 +49,10 @@ config: level: debug drivername: console user: - console: - drivertype: console - level: debug - drivername: console + file: + # drivertype 为驱动类型 file-文件 db-数据库 console-控制台 + drivertype: file + level: info + # drivername 需要和output的map 键值相同 + drivername: user diff --git a/infra/logger/file.go b/infra/logger/file.go index f6e1201..862dc0d 100644 --- a/infra/logger/file.go +++ b/infra/logger/file.go @@ -1,6 +1,8 @@ package logger import ( + "strconv" + //"github.com/natefinch/lumberjack" rotatelogs "github.com/lestrrat-go/file-rotatelogs" "go.uber.org/zap" @@ -9,7 +11,7 @@ import ( ) var ( - fileLogCore *zap.Logger + fileLogCoreMap map[string]*zap.Logger ) type fileDriver struct { @@ -18,7 +20,15 @@ type fileDriver struct { } func NewFileDriver(config file, level int) (driver, error) { - if fileLogCore == nil { + var logCore *zap.Logger + if fileLogCoreMap == nil { + fileLogCoreMap = make(map[string]*zap.Logger) + } + + mKey := config.Filename+strconv.Itoa(config.MaxAge) + logCore = fileLogCoreMap[mKey] + if logCore == nil { + //lumberjackLogger := &lumberjack.Logger{ // Filename: config.Filename, // MaxSize: config.MaxSize, @@ -26,7 +36,7 @@ func NewFileDriver(config file, level int) (driver, error) { // MaxBackups: config.MaxBackups, // Compress: config.Compress, //} - age := time.Hour*24*time.Duration(config.MaxAge) + age := time.Hour * 24 * time.Duration(config.MaxAge) hook, err := rotatelogs.New( config.Filename+".%Y%m%d", rotatelogs.WithLinkName(config.Filename), @@ -42,9 +52,10 @@ func NewFileDriver(config file, level int) (driver, error) { coreEncoder.EncodeLevel = zapcore.CapitalLevelEncoder encoder := zapcore.NewJSONEncoder(coreEncoder) core := zapcore.NewCore(encoder, writeSync, zap.DebugLevel) - fileLogCore = zap.New(core) + logCore = zap.New(core) + fileLogCoreMap[mKey] = logCore } - return &fileDriver{level, fileLogCore}, nil + return &fileDriver{level, logCore}, nil } func (f *fileDriver) Record(message message) {