mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-24 03:46:24 +00:00
logger.WithReportCaller: allow custom depth
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
parent
ef3af1e4d9
commit
724d6103c6
@ -55,7 +55,7 @@ type Level = logrus.Level
|
|||||||
type Logger interface {
|
type Logger interface {
|
||||||
WithContext(ctx context.Context) Logger
|
WithContext(ctx context.Context) Logger
|
||||||
|
|
||||||
WithReportCaller(b bool) Logger
|
WithReportCaller(b bool, depth ...uint) Logger
|
||||||
|
|
||||||
WithField(key string, value interface{}) Logger
|
WithField(key string, value interface{}) Logger
|
||||||
WithFields(kv ...interface{}) Logger
|
WithFields(kv ...interface{}) Logger
|
||||||
@ -105,7 +105,7 @@ type Logger interface {
|
|||||||
|
|
||||||
type logger struct {
|
type logger struct {
|
||||||
fl logrus.Ext1FieldLogger
|
fl logrus.Ext1FieldLogger
|
||||||
reportCaller bool
|
reportCaller int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Tracef(format string, args ...interface{}) {
|
func (l *logger) Tracef(format string, args ...interface{}) {
|
||||||
@ -251,8 +251,18 @@ func (l *logger) WithError(err error) Logger {
|
|||||||
return &logger{fl: l.fl.WithError(err), reportCaller: l.reportCaller}
|
return &logger{fl: l.fl.WithError(err), reportCaller: l.reportCaller}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) WithReportCaller(b bool) Logger {
|
func (l *logger) WithReportCaller(b bool, depth ...uint) Logger {
|
||||||
return &logger{fl: l.fl, reportCaller: b}
|
var d int
|
||||||
|
if b {
|
||||||
|
if len(depth) > 0 {
|
||||||
|
d = int(depth[0])
|
||||||
|
} else {
|
||||||
|
d = 0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
d = -1
|
||||||
|
}
|
||||||
|
return &logger{fl: l.fl, reportCaller: d}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Logr() logr.Logger {
|
func (l *logger) Logr() logr.Logger {
|
||||||
@ -300,11 +310,11 @@ func (l *logger) Clone() Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) withCaller() logrus.Ext1FieldLogger {
|
func (l *logger) withCaller() logrus.Ext1FieldLogger {
|
||||||
if !l.reportCaller {
|
if l.reportCaller == -1 {
|
||||||
return l.fl
|
return l.fl
|
||||||
}
|
}
|
||||||
pcs := make([]uintptr, 1)
|
pcs := make([]uintptr, 1)
|
||||||
runtime.Callers(3, 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())
|
||||||
|
Loading…
Reference in New Issue
Block a user