logger.WithReportCaller: fix enabled by default

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2023-08-11 19:34:42 +02:00
parent 724d6103c6
commit b52ae2c670
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF

View File

@ -105,7 +105,7 @@ type Logger interface {
type logger struct { type logger struct {
fl logrus.Ext1FieldLogger fl logrus.Ext1FieldLogger
reportCaller int reportCaller *int
} }
func (l *logger) Tracef(format string, args ...interface{}) { func (l *logger) Tracef(format string, args ...interface{}) {
@ -252,17 +252,16 @@ func (l *logger) WithError(err error) Logger {
} }
func (l *logger) WithReportCaller(b bool, depth ...uint) Logger { func (l *logger) WithReportCaller(b bool, depth ...uint) Logger {
if !b {
return &logger{fl: l.fl}
}
var d int var d int
if b {
if len(depth) > 0 { if len(depth) > 0 {
d = int(depth[0]) d = int(depth[0])
} else { } else {
d = 0 d = 0
} }
} else { return &logger{fl: l.fl, reportCaller: &d}
d = -1
}
return &logger{fl: l.fl, reportCaller: d}
} }
func (l *logger) Logr() logr.Logger { func (l *logger) Logr() logr.Logger {
@ -310,11 +309,11 @@ func (l *logger) Clone() Logger {
} }
func (l *logger) withCaller() logrus.Ext1FieldLogger { func (l *logger) withCaller() logrus.Ext1FieldLogger {
if l.reportCaller == -1 { if l.reportCaller == nil {
return l.fl return l.fl
} }
pcs := make([]uintptr, 1) pcs := make([]uintptr, 1)
runtime.Callers(3+l.reportCaller, pcs) runtime.Callers(3+*l.reportCaller, pcs)
f, _ := runtime.CallersFrames(pcs).Next() f, _ := runtime.CallersFrames(pcs).Next()
pkg := getPackageName(f.Function) pkg := getPackageName(f.Function)
return l.fl.WithField("caller", fmt.Sprintf("%s/%s:%d", pkg, filepath.Base(f.File), f.Line)).WithField("func", f.Func.Name()) return l.fl.WithField("caller", fmt.Sprintf("%s/%s:%d", pkg, filepath.Base(f.File), f.Line)).WithField("func", f.Func.Name())