Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 2fb9484

Browse files
committed
[FAB-6342] DefaultLogger can print logger as caller
Change-Id: I80b272ad8a1f4cded69a069d372dc98afed16e0e Signed-off-by: Troy Ronda <troy.ronda@securekey.com>
1 parent 50bab2e commit 2fb9484

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

pkg/logging/logger.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"runtime"
15+
"strings"
1516

1617
"sync"
1718

@@ -370,19 +371,34 @@ func (l *DefaultLogger) logln(level Level, args ...interface{}) {
370371
l.defaultLogger.Output(2, customPrefix+fmt.Sprintln(args...))
371372
}
372373

373-
// getCaller utility to find caller function used to mention in log lines
374374
func (l *DefaultLogger) getCaller() string {
375-
fpcs := make([]uintptr, 1)
376-
// skip 3 levels to get to the caller of whoever called getCaller()
377-
n := runtime.Callers(4, fpcs)
375+
const MAXCALLERS = 5 // search MAXCALLERS frames for the real caller
376+
const SKIPCALLERS = 4 // skip SKIPCALLERS frames when determining the real caller
377+
const LOGPREFIX = "logging.(*Logger)" // LOGPREFIX indicates the upcoming frame contains the real caller and skip the frame
378+
const LOGBRIDGEPREFIX = "logbridge." // LOGBRIDGEPREFIX indicates to skip the frame due to being a logbridge
379+
const NOTFOUND = "n/a"
380+
381+
fpcs := make([]uintptr, MAXCALLERS)
382+
383+
n := runtime.Callers(SKIPCALLERS, fpcs)
378384
if n == 0 {
379-
return "n/a"
385+
return NOTFOUND
380386
}
381387

382-
fun := runtime.FuncForPC(fpcs[0] - 1)
383-
if fun == nil {
384-
return "n/a"
388+
frames := runtime.CallersFrames(fpcs[:n])
389+
funcIsNext := false
390+
for f, more := frames.Next(); more; f, more = frames.Next() {
391+
_, funName := filepath.Split(f.Function)
392+
if f.Func == nil || f.Function == "" {
393+
funName = NOTFOUND // not a function or unknown
394+
}
395+
396+
if strings.HasPrefix(funName, LOGPREFIX) || strings.HasPrefix(funName, LOGBRIDGEPREFIX) {
397+
funcIsNext = true
398+
} else if funcIsNext {
399+
return funName
400+
}
385401
}
386-
_, funName := filepath.Split(fun.Name())
387-
return funName
402+
403+
return NOTFOUND
388404
}

0 commit comments

Comments
 (0)