From 33621e61e16aeda4dc6c9492d29e08cf13a2e9e2 Mon Sep 17 00:00:00 2001 From: cc Date: Sun, 12 Nov 2023 15:21:04 +0000 Subject: [PATCH] =?UTF-8?q?zaplog=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 10 ++++++ go.sum | 16 +++++++++ log/zaplog/config.go | 61 ++++++++++++++++++++++++++++++++ log/zaplog/zaplog.go | 84 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 log/zaplog/config.go create mode 100644 log/zaplog/zaplog.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..104d29e --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module git.shikicc.com/golang/kit + +go 1.21.4 + +require ( + go.uber.org/zap v1.26.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 +) + +require go.uber.org/multierr v1.10.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2917eda --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +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/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/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log/zaplog/config.go b/log/zaplog/config.go new file mode 100644 index 0000000..8f55be6 --- /dev/null +++ b/log/zaplog/config.go @@ -0,0 +1,61 @@ +package zaplog + +type LogFileConfig struct { + Filename string // 日志文件存放目录及名称,如果文件夹不存在会自动创建 + FileSize_MB int // 文件大小限制,单位MB + FileBackup int // 最大保留日志文件数量 + FileAge_DAY int // 日志文件保留天数 +} + +type ZapConfig struct { + JsonFormat bool // 是否使用json格式,默认false + NormalLog LogFileConfig // 普通日志配置 + ErrorLog LogFileConfig // 错误日志配置 +} + +var cfg_default = ZapConfig{ + NormalLog: LogFileConfig{ + Filename: "./log/normal_log", + FileSize_MB: 5, + FileBackup: 5, + FileAge_DAY: 30, + }, + ErrorLog: LogFileConfig{ + Filename: "./log/error_log", + FileSize_MB: 5, + FileBackup: 5, + FileAge_DAY: 30, + }, +} + +func configDefault(config ...ZapConfig) ZapConfig { + if len(config) < 1 { + return cfg_default + } + cfg := config[0] + if cfg.NormalLog.Filename == "" { + cfg.NormalLog.Filename = cfg_default.NormalLog.Filename + } + if cfg.NormalLog.FileSize_MB == 0 { + cfg.NormalLog.FileSize_MB = cfg_default.NormalLog.FileSize_MB + } + if cfg.NormalLog.FileBackup == 0 { + cfg.NormalLog.FileBackup = cfg_default.NormalLog.FileBackup + } + if cfg.NormalLog.FileAge_DAY == 0 { + cfg.NormalLog.FileAge_DAY = cfg_default.NormalLog.FileAge_DAY + } + if cfg.ErrorLog.Filename == "" { + cfg.ErrorLog.Filename = cfg_default.ErrorLog.Filename + } + if cfg.ErrorLog.FileSize_MB == 0 { + cfg.ErrorLog.FileSize_MB = cfg_default.ErrorLog.FileSize_MB + } + if cfg.ErrorLog.FileBackup == 0 { + cfg.ErrorLog.FileBackup = cfg_default.ErrorLog.FileBackup + } + if cfg.ErrorLog.FileAge_DAY == 0 { + cfg.ErrorLog.FileAge_DAY = cfg_default.ErrorLog.FileAge_DAY + } + return cfg +} diff --git a/log/zaplog/zaplog.go b/log/zaplog/zaplog.go new file mode 100644 index 0000000..5c36245 --- /dev/null +++ b/log/zaplog/zaplog.go @@ -0,0 +1,84 @@ +package zaplog + +import ( + "os" + "time" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" +) + +func textencoderConfig() zapcore.EncoderConfig { + encoderConfig := zap.NewProductionEncoderConfig() + //时间格式 + encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]") + } + //日志等级字母大写 + encoderConfig.EncodeLevel = func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString("[" + level.CapitalString() + "]") + } + return encoderConfig +} + +func jsonencoderConfig() zapcore.EncoderConfig { + encoderConfig := zap.NewProductionEncoderConfig() + //时间格式 + encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("2006-01-02 15:04:05")) + } + //日志等级字母大写 + encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder + return encoderConfig +} + +func New(config ...ZapConfig) *zap.SugaredLogger { + cfg := configDefault(config...) + + var encoder zapcore.Encoder + encoder = zapcore.NewConsoleEncoder(textencoderConfig()) + if cfg.JsonFormat { + encoder = zapcore.NewJSONEncoder(jsonencoderConfig()) + } + + //日志级别 error 级别 + highPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev >= zap.ErrorLevel + }) + //info 和 debug 级别,debug 级别是最低的 + lowPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev < zap.ErrorLevel && lev >= zap.DebugLevel + }) + //info文件 writeSyncer + infoFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{ + Filename: cfg.NormalLog.Filename, + MaxSize: cfg.NormalLog.FileSize_MB, + MaxBackups: cfg.NormalLog.FileBackup, + MaxAge: cfg.NormalLog.FileAge_DAY, + Compress: true, + }) + + //第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志 + infoFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(infoFileWriteSyncer, + zapcore.AddSync(os.Stdout)), lowPriority) + + //error文件writeSyncer + errorFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{ + Filename: cfg.ErrorLog.Filename, + MaxSize: cfg.ErrorLog.FileSize_MB, + MaxBackups: cfg.ErrorLog.FileBackup, + MaxAge: cfg.ErrorLog.FileAge_DAY, + Compress: true, + }) + + //第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志 + errorFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(errorFileWriteSyncer, + zapcore.AddSync(os.Stdout)), highPriority) + var coreArr []zapcore.Core + coreArr = append(coreArr, infoFileCore) + coreArr = append(coreArr, errorFileCore) + //zap.AddCaller()为显示文件名和行号,可省略 + // return zap.New(zapcore.NewTee(coreArr...), zap.AddCaller()) + return zap.New(zapcore.NewTee(coreArr...)).Sugar() +}