diff --git a/logger/config.go b/logger/config.go index 2d32d13..094a4aa 100644 --- a/logger/config.go +++ b/logger/config.go @@ -241,11 +241,43 @@ type OptionLogger struct { UseShortCaller bool // 使用短的调用文件格式 TimeEncoder zapcore.TimeEncoder // 格式化时间的函数 EncodeDuration zapcore.DurationEncoder // 原始时间信息 + WithCaller bool // 是否打印文件行号 + WithCallerSkip int // 跳过的调用数 + ConsoleOutput bool // 控制台输出 + Encoder zapcore.Encoder // 编码函数 } -// 设置日志配置 +// SetLoggerOptionFunc 设置日志配置 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格式数据 // // Author : go_developer@163.com<张德满> diff --git a/logger/logger.go b/logger/logger.go index 6795080..0581e20 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -25,19 +25,26 @@ import ( // Author : go_developer@163.com<张德满> // // 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 { 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 { return lvl >= loggerLevel }) l := &Logger{ splitConfig: splitConfig, - encoder: encoder, + encoder: o.Encoder, } var ( err error @@ -49,19 +56,19 @@ func NewLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En } fileHandlerList := []zapcore.Core{ - zapcore.NewCore(encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal), + zapcore.NewCore(o.Encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal), } // 设置控制台输出 - if consoleOutput { - fileHandlerList = append(fileHandlerList, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal)) + if o.ConsoleOutput { + fileHandlerList = append(fileHandlerList, zapcore.NewCore(o.Encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal)) } // 最后创建具体的Logger core := zapcore.NewTee(fileHandlerList...) // 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数, 跳过一行可以直接显示业务代码行号,否则显示日志包行号 - log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(-1)) + log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(3)) return log, nil } diff --git a/logger/logger_test.go b/logger/logger_test.go index 4fff456..39dbfde 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -23,12 +23,11 @@ import ( // // Date : 5:50 下午 2021/1/2 func Test_Logger(t *testing.T) { - encoder := GetEncoder(WithTimeEncoder(MsTimeEncoder)) c, err := NewRotateLogConfig("./logs", "test.log", WithTimeIntervalType(TimeIntervalTypeMinute), WithMaxAge(120*time.Second)) if nil != err { panic(err) } - l, err := NewLogger(zapcore.InfoLevel, true, encoder, c) + l, err := NewLogger(zapcore.InfoLevel, c, WithCallerSkip(1), WithConsoleOutput()) if nil != err { panic(err) } diff --git a/logger/wrapper/gorm_v2.go b/logger/wrapper/gorm_v2.go index f749210..908b0db 100644 --- a/logger/wrapper/gorm_v2.go +++ b/logger/wrapper/gorm_v2.go @@ -28,7 +28,11 @@ import ( // // Date : 9:56 下午 2021/3/1 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 { return nil, err } @@ -36,7 +40,8 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En traceIDField = "trace_id" } return &Gorm{ - instance: logInstance, + instance: logInstance, + traceIDField: traceIDField, }, 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.Error(err), ) - return } // getTraceID 获取traceID diff --git a/logger/wrapper/http_gin.go b/logger/wrapper/http_gin.go index 0628417..22052df 100644 --- a/logger/wrapper/http_gin.go +++ b/logger/wrapper/http_gin.go @@ -12,6 +12,7 @@ import ( "github.com/gin-gonic/gin" "github.com/go-developer/gopkg/logger" + logger2 "github.com/go-developer/gopkg/logger" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -26,7 +27,11 @@ func NewGinLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore err error 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 }