升级包装的日志库

This commit is contained in:
白茶清欢 2021-03-05 00:41:33 +08:00
parent c663438a27
commit d539f84737
5 changed files with 62 additions and 15 deletions

View File

@ -241,11 +241,43 @@ type OptionLogger struct {
UseShortCaller bool // 使用短的调用文件格式 UseShortCaller bool // 使用短的调用文件格式
TimeEncoder zapcore.TimeEncoder // 格式化时间的函数 TimeEncoder zapcore.TimeEncoder // 格式化时间的函数
EncodeDuration zapcore.DurationEncoder // 原始时间信息 EncodeDuration zapcore.DurationEncoder // 原始时间信息
WithCaller bool // 是否打印文件行号
WithCallerSkip int // 跳过的调用数
ConsoleOutput bool // 控制台输出
Encoder zapcore.Encoder // 编码函数
} }
// 设置日志配置 // SetLoggerOptionFunc 设置日志配置
type SetLoggerOptionFunc func(o *OptionLogger) type SetLoggerOptionFunc func(o *OptionLogger)
// WithCaller 打开文件行号记录
func WithCaller() SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.WithCaller = true
}
}
// WithCallerSkip ...
func WithCallerSkip(skip int) SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.WithCallerSkip = skip
}
}
// WithConsoleOutput 日志控制台输出
func WithConsoleOutput() SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.ConsoleOutput = true
}
}
// WithEncoder ...
func WithEncoder(encoder zapcore.Encoder) SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.Encoder = encoder
}
}
// WithUseJsonFormat 日志是否使用json格式数据 // WithUseJsonFormat 日志是否使用json格式数据
// //
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>

View File

@ -25,19 +25,26 @@ import (
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 5:05 下午 2021/1/2 // Date : 5:05 下午 2021/1/2
func NewLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *RotateLogConfig) (*zap.Logger, error) { func NewLogger(loggerLevel zapcore.Level, splitConfig *RotateLogConfig, optionFunc ...SetLoggerOptionFunc) (*zap.Logger, error) {
if nil == splitConfig { if nil == splitConfig {
return nil, errors.New("未配置日志切割规则") return nil, errors.New("未配置日志切割规则")
} }
if nil == encoder {
encoder = GetEncoder() o := &OptionLogger{}
for _, f := range optionFunc {
f(o)
}
if nil == o.Encoder {
o.Encoder = GetEncoder()
} }
loggerLevelDeal := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { loggerLevelDeal := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= loggerLevel return lvl >= loggerLevel
}) })
l := &Logger{ l := &Logger{
splitConfig: splitConfig, splitConfig: splitConfig,
encoder: encoder, encoder: o.Encoder,
} }
var ( var (
err error err error
@ -49,19 +56,19 @@ func NewLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En
} }
fileHandlerList := []zapcore.Core{ fileHandlerList := []zapcore.Core{
zapcore.NewCore(encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal), zapcore.NewCore(o.Encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal),
} }
// 设置控制台输出 // 设置控制台输出
if consoleOutput { if o.ConsoleOutput {
fileHandlerList = append(fileHandlerList, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal)) fileHandlerList = append(fileHandlerList, zapcore.NewCore(o.Encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal))
} }
// 最后创建具体的Logger // 最后创建具体的Logger
core := zapcore.NewTee(fileHandlerList...) core := zapcore.NewTee(fileHandlerList...)
// 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数, 跳过一行可以直接显示业务代码行号,否则显示日志包行号 // 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数, 跳过一行可以直接显示业务代码行号,否则显示日志包行号
log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(-1)) log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(3))
return log, nil return log, nil
} }

View File

@ -23,12 +23,11 @@ import (
// //
// Date : 5:50 下午 2021/1/2 // Date : 5:50 下午 2021/1/2
func Test_Logger(t *testing.T) { func Test_Logger(t *testing.T) {
encoder := GetEncoder(WithTimeEncoder(MsTimeEncoder))
c, err := NewRotateLogConfig("./logs", "test.log", WithTimeIntervalType(TimeIntervalTypeMinute), WithMaxAge(120*time.Second)) c, err := NewRotateLogConfig("./logs", "test.log", WithTimeIntervalType(TimeIntervalTypeMinute), WithMaxAge(120*time.Second))
if nil != err { if nil != err {
panic(err) panic(err)
} }
l, err := NewLogger(zapcore.InfoLevel, true, encoder, c) l, err := NewLogger(zapcore.InfoLevel, c, WithCallerSkip(1), WithConsoleOutput())
if nil != err { if nil != err {
panic(err) panic(err)
} }

View File

@ -28,7 +28,11 @@ import (
// //
// Date : 9:56 下午 2021/3/1 // Date : 9:56 下午 2021/3/1
func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger2.RotateLogConfig, traceIDField string) (logger.Interface, error) { func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger2.RotateLogConfig, traceIDField string) (logger.Interface, error) {
logInstance, err := logger2.NewLogger(loggerLevel, consoleOutput, encoder, splitConfig) logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder)}
if consoleOutput {
logConfList = append(logConfList, logger2.WithConsoleOutput())
}
logInstance, err := logger2.NewLogger(loggerLevel, splitConfig, logConfList...)
if nil != err { if nil != err {
return nil, err return nil, err
} }
@ -36,7 +40,8 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En
traceIDField = "trace_id" traceIDField = "trace_id"
} }
return &Gorm{ return &Gorm{
instance: logInstance, instance: logInstance,
traceIDField: traceIDField,
}, nil }, nil
} }
@ -127,7 +132,6 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in
zap.Int64("affect_rows", affectRows), zap.Int64("affect_rows", affectRows),
zap.Error(err), zap.Error(err),
) )
return
} }
// getTraceID 获取traceID // getTraceID 获取traceID

View File

@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-developer/gopkg/logger" "github.com/go-developer/gopkg/logger"
logger2 "github.com/go-developer/gopkg/logger"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
@ -26,7 +27,11 @@ func NewGinLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore
err error err error
l *zap.Logger l *zap.Logger
) )
if l, err = logger.NewLogger(loggerLevel, consoleOutput, encoder, splitConfig); nil != err { logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder)}
if consoleOutput {
logConfList = append(logConfList, logger2.WithConsoleOutput())
}
if l, err = logger.NewLogger(loggerLevel, splitConfig, logConfList...); nil != err {
return nil, err return nil, err
} }