From 38a2e3b582e07e20f98cdf4563ffe9fb00223d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Fri, 5 Mar 2021 00:46:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7wrapper=E7=9A=84=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger/logger.go | 6 +++++- logger/wrapper/gorm_v2.go | 2 +- logger/wrapper/http_gin.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 0581e20..84757c5 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -68,7 +68,11 @@ func NewLogger(loggerLevel zapcore.Level, splitConfig *RotateLogConfig, optionFu core := zapcore.NewTee(fileHandlerList...) // 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数, 跳过一行可以直接显示业务代码行号,否则显示日志包行号 - log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(3)) + logConfList := []zap.Option{} + if o.WithCaller { + logConfList = append(logConfList, zap.AddCaller(), zap.AddCallerSkip(o.WithCallerSkip)) + } + log := zap.New(core, logConfList...) return log, nil } diff --git a/logger/wrapper/gorm_v2.go b/logger/wrapper/gorm_v2.go index 908b0db..cb94ed0 100644 --- a/logger/wrapper/gorm_v2.go +++ b/logger/wrapper/gorm_v2.go @@ -28,7 +28,7 @@ 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) { - logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder)} + logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder), logger2.WithCallerSkip(3), logger2.WithCaller()} if consoleOutput { logConfList = append(logConfList, logger2.WithConsoleOutput()) } diff --git a/logger/wrapper/http_gin.go b/logger/wrapper/http_gin.go index 22052df..64df301 100644 --- a/logger/wrapper/http_gin.go +++ b/logger/wrapper/http_gin.go @@ -27,7 +27,7 @@ func NewGinLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore err error l *zap.Logger ) - logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder)} + logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder), logger2.WithCaller(), logger2.WithCallerSkip(1)} if consoleOutput { logConfList = append(logConfList, logger2.WithConsoleOutput()) }