Skip to content

Commit

Permalink
Added AddSkipPackageFromStackTrace API to skip frames from the caller…
Browse files Browse the repository at this point in the history
… stacktrace.
  • Loading branch information
alexstol committed Dec 10, 2019
1 parent 67a7fdc commit 04ae225
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
19 changes: 14 additions & 5 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var (
bufferPool *sync.Pool

// qualified package name, cached at first use
logrusPackage string
//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 @@ -147,6 +148,12 @@ func (entry *Entry) WithTime(t time.Time) *Entry {
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err, Context: entry.Context}
}

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

// getPackageName reduces a fully qualified function name to the package name
// There really ought to be to be a better way...
func getPackageName(f string) string {
Expand All @@ -170,7 +177,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 +191,12 @@ 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 pkg != logrusPackage {a
if _, has := skipPackageNameForCaller[getPackageName(f.Function)]; !has {
return &f
}
}

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

// AddSkipPackageFromStackTrace ...
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 ...
func (logger *Logger) SetReportCaller(reportCaller bool) {
logger.mu.Lock()
defer logger.mu.Unlock()
logger.ReportCaller = reportCaller
}

// AddSkipPackageFromStackTrace ...
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

0 comments on commit 04ae225

Please sign in to comment.