From 403a9354d763b680f36fa01dabbb56c4abed1d87 Mon Sep 17 00:00:00 2001 From: kanade Date: Fri, 11 Aug 2023 11:58:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=88=86=E5=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 7 +------ go.sum | 11 ----------- logger.go | 57 +++++++++++++++++++------------------------------------ 3 files changed, 20 insertions(+), 55 deletions(-) diff --git a/go.mod b/go.mod index fc10463..d1c19ab 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,4 @@ module git.oa00.com/go/logger go 1.16 -require ( - github.com/jonboulle/clockwork v0.4.0 // indirect - github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible - github.com/lestrrat-go/strftime v1.0.6 // indirect - go.uber.org/zap v1.25.0 -) +require go.uber.org/zap v1.25.0 diff --git a/go.sum b/go.sum index b6eb811..df05950 100644 --- a/go.sum +++ b/go.sum @@ -4,26 +4,15 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= -github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8= -github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is= -github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4= -github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= -github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ= -github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= diff --git a/logger.go b/logger.go index a5c307a..cc8a004 100644 --- a/logger.go +++ b/logger.go @@ -1,12 +1,10 @@ package logger import ( - "fmt" - zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "io" "os" - "path" "time" ) @@ -47,25 +45,18 @@ type logger struct { } type LoggerConfig struct { - Director string `json:"director"` - Levels []zapcore.Level `json:"level"` - ShowLine bool `json:"showLine"` - StacktraceKey string `json:"stacktraceKey"` - LinkName string `json:"linkName"` - LogInConsole bool `json:"logInConsole"` - Format string `json:"format"` - EncodeLevel zapcore.LevelEncoder `json:"encodeLevel"` - Prefix string `json:"prefix"` - MaxAge uint `json:"maxAge"` + Levels []zapcore.Level `json:"level"` + ShowLine bool `json:"showLine"` + LogInConsole bool `json:"logInConsole"` + Format string `json:"format"` + EncodeLevel zapcore.LevelEncoder `json:"encodeLevel"` + Prefix string `json:"prefix"` + Writer func(config *LoggerConfig, filename string, level zapcore.Level) io.Writer } // InitLogger @Title 初始化日志工具 func InitLogger(config *LoggerConfig) *logger { Logger.Config = config - if ok, _ := Logger.pathExists(config.Director); !ok { // 判断是否有Director文件夹 - fmt.Printf("create %v directory\n", config.Director) - _ = os.Mkdir(config.Director, os.ModePerm) - } Logger.Logger = zap.New(zapcore.NewTee( Logger.getEncoderCore("debug", zapcore.DebugLevel), @@ -87,7 +78,6 @@ func (l *logger) getEncoderConfig() (config zapcore.EncoderConfig) { TimeKey: "time", NameKey: "logger", CallerKey: "caller", - StacktraceKey: l.Config.StacktraceKey, LineEnding: zapcore.DefaultLineEnding, EncodeLevel: l.Config.EncodeLevel, EncodeTime: l.CustomTimeEncoder, @@ -110,12 +100,7 @@ func (l *logger) getEncoder() zapcore.Encoder { // getEncoderCore 获取Encoder的zapcore.Core func (l *logger) getEncoderCore(filename string, level zapcore.Level) (core zapcore.Core) { - writer, err := l.getWriteSyncer(filename) // 使用file-rotatelogs进行日志分割 - if err != nil { - fmt.Printf("Get Write Syncer Failed err:%v", err.Error()) - return - } - return zapcore.NewCore(l.getEncoder(), writer, zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { + return zapcore.NewCore(l.getEncoder(), l.getWriteSyncer(filename, level), zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return l.inLevel(level) && lvl >= level })) } @@ -150,21 +135,17 @@ func (l *logger) pathExists(path string) (bool, error) { return false, err } -func (l *logger) getWriteSyncer(filename string) (zapcore.WriteSyncer, error) { - options := []zaprotatelogs.Option{ - zaprotatelogs.WithLinkName(l.Config.LinkName), - zaprotatelogs.WithRotationTime(time.Hour * 24), - } - if l.Config.MaxAge > 0 { - options = append(options, zaprotatelogs.WithMaxAge(time.Hour*24*time.Duration(l.Config.MaxAge))) +func (l *logger) getWriteSyncer(filename string, level zapcore.Level) zapcore.WriteSyncer { + var fileWriter io.Writer + // 日志切割 + if l.Config.Writer != nil { + fileWriter = l.Config.Writer(l.Config, filename, level) + } else { + return zapcore.AddSync(os.Stdout) } - fileWriter, err := zaprotatelogs.New( - path.Join(l.Config.Director, filename+".%Y-%m-%d.log"), - options..., - ) - if l.Config.LogInConsole && !l.hasShowLine { + if l.Config.LogInConsole && !l.hasShowLine && l.inLevel(level) { l.hasShowLine = true - return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err + return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)) } - return zapcore.AddSync(fileWriter), err + return zapcore.AddSync(fileWriter) }