Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add skipPackageNameForCaller #898

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
bufferPool *sync.Pool

// qualified package name, cached at first use
logrusPackage string
skipPackageNameForCaller = make(map[string]struct{}, 1)

// Positions in the call stack when tracing to report the calling method
minimumCallerDepth int
Expand All @@ -41,6 +41,12 @@ func init() {
minimumCallerDepth = 1
}

// Set the global qualified package name.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the examples here as well.

// ex: logrus.AddSkipPackageFromStackTrace("github.com/go-xorm/xorm")
func AddSkipPackageFromStackTrace(name string) {
skipPackageNameForCaller[name] = struct{}{}
}

// Defines the key when adding errors using WithError.
var ErrorKey = "error"

Expand Down Expand Up @@ -168,13 +174,13 @@ func getCaller() *runtime.Frame {

// cache this package's fully-qualified name
callerInitOnce.Do(func() {
pcs := make([]uintptr, 2)
pcs := make([]uintptr, 2)
_ = runtime.Callers(0, pcs)
logrusPackage = getPackageName(runtime.FuncForPC(pcs[1]).Name())
AddSkipPackageFromStackTrace(getPackageName(runtime.FuncForPC(pcs[1]).Name()))

// 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
minimumCallerDepth = knownLogrusFrames
minimumCallerDepth = knownLogrusFrames
})

// Restrict the lookback frames to avoid runaway lookups
Expand All @@ -183,10 +189,9 @@ func getCaller() *runtime.Frame {
frames := runtime.CallersFrames(pcs[:depth])

for f, again := frames.Next(); again; f, again = frames.Next() {
pkg := getPackageName(f.Function)


// If the caller isn't part of this package, we're done
if pkg != logrusPackage {
if _, has := skipPackageNameForCaller[getPackageName(f.Function)]; !has {
return &f
}
}
Expand Down