Skip to content

Merging AddSkipPackageFromStackTrace API #1079

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 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 Down Expand Up @@ -170,7 +170,8 @@ func getCaller() *runtime.Frame {
callerInitOnce.Do(func() {
pcs := make([]uintptr, 2)
_ = runtime.Callers(0, pcs)
logrusPackage = getPackageName(runtime.FuncForPC(pcs[1]).Name())
//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
Expand All @@ -183,11 +184,11 @@ func getCaller() *runtime.Frame {
frames := runtime.CallersFrames(pcs[:depth])

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

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

Expand Down
7 changes: 7 additions & 0 deletions exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func SetReportCaller(include bool) {
std.SetReportCaller(include)
}

// AddSkipPackageFromStackTrace excludes package names from the caller report when
// SetReportCaller set to true. Commonly used to skip a custom logrus wrapper package frames.
// ex. log.AddSkipPackageFromStackTrace("mycoolapp/mylogger") // skip mycoolapp/mylogger wrapper.
func AddSkipPackageFromStackTrace(name string) {
std.AddSkipPackageFromStackTrace(name)
}

// SetLevel sets the standard logger level.
func SetLevel(level Level) {
std.SetLevel(level)
Expand Down
6 changes: 6 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,18 @@ func (logger *Logger) SetOutput(output io.Writer) {
logger.Out = output
}

// SetReportCaller logs the caller stack frame.
func (logger *Logger) SetReportCaller(reportCaller bool) {
logger.mu.Lock()
defer logger.mu.Unlock()
logger.ReportCaller = reportCaller
}

// AddSkipPackageFromStackTrace excludes a package from the caller report.
func (logger *Logger) AddSkipPackageFromStackTrace(name string) {
skipPackageNameForCaller[name] = struct{}{}
}

// ReplaceHooks replaces the logger hooks and returns the old ones
func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
logger.mu.Lock()
Expand Down