From af6ac8cee616a8cd9f2b2afaa78dc66c7d420495 Mon Sep 17 00:00:00 2001 From: Fabrizio Cirelli Date: Mon, 9 Mar 2020 12:36:52 +0100 Subject: [PATCH] Fix wrong caller --- entry.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/entry.go b/entry.go index 1bf127c..defb8bc 100644 --- a/entry.go +++ b/entry.go @@ -180,15 +180,20 @@ func getPackageName(f string) string { // getCaller retrieves the name of the first non-logrus calling function func getCaller() *runtime.Frame { - // cache this package's fully-qualified name callerInitOnce.Do(func() { - pcs := make([]uintptr, 2) + pcs := make([]uintptr, maximumCallerDepth) _ = runtime.Callers(0, pcs) - logrusPackage = getPackageName(funcName(pcs)) - // now that we have the cache, we can skip a minimum count of known-logrus functions - // XXX this is dubious, the number of frames may vary + // dynamic get the package name and the minimum caller depth + for i := 0; i < maximumCallerDepth; i++ { + funcName := runtime.FuncForPC(pcs[i]).Name() + if strings.Contains(funcName, "getCaller") { + logrusPackage = getPackageName(funcName) + break + } + } + minimumCallerDepth = knownLogrusFrames })