logrus icon indicating copy to clipboard operation
logrus copied to clipboard

add caller skip

Open dongshimou opened this issue 5 years ago • 8 comments

can help me skip some stack

dongshimou avatar May 23 '19 06:05 dongshimou

Thanks. I also need this feature. No need to implement

sirupsen/logrus/exported.go need SetCallerSkip funcation

rentiansheng avatar Jun 04 '19 07:06 rentiansheng

Why not depth := runtime.Callers(minimumCallerDepth + skip, pcs) rather than using if statement?

better:

		pcs := make([]uintptr, 2)
		_ = runtime.Callers(0, pcs)
		logrusPackage = 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 + skip
	})```

kync avatar Sep 26 '19 13:09 kync

I am interested in this as well. Anything blocking it moving forward?

updogliu avatar Sep 21 '20 13:09 updogliu

Hi @dongshimou,

One proposal, adding these codes to align with other params:

func (logger *Logger) SetCallerSkip(skip int) {
	logger.mu.Lock()
	defer logger.mu.Unlock()
	logger.SetCallerSkip = skip
}

func SetCallerSkip(skip int) {
	std.SetCallerSkip(skip)
}

xqzhang2015 avatar Dec 04 '20 08:12 xqzhang2015

BTW, maybe the system-level logger maybe not enough. One option is to define func at call-level, like

  • DebugDepth(depth int, args ...interface{})
  • InfoDepth(depth int, args ...interface{})
  • ...

xqzhang2015 avatar Dec 04 '20 08:12 xqzhang2015

This is a feature I would like to have but It seems there is no intention to include new features in the project.

I ended up using the CallerPrettyfier field in the JSONFormatter:

formatter := &logrus.JSONFormatter{
        // ...
	CallerPrettyfier: callerFinder,
	// ...
}

func callerFinder(f *runtime.Frame) (function string, file string) {
	path, err := os.Getwd()
	if err == nil {
		pcs := make([]uintptr, 50)
		_ = runtime.Callers(0, pcs)
		frames := runtime.CallersFrames(pcs)

		for next, again := frames.Next(); again; next, again = frames.Next() {
			if strings.HasPrefix(next.File, path) {
				return next.Function, fmt.Sprintf("%s:%d", next.File, next.Line)
			}
		}
	}

	return f.Function, fmt.Sprintf("%s:%d", f.File, f.Line)
}

I know it's not beautiful, and I would prefer a feature for it, but it works to get the information I need.

dvdalilue avatar Sep 15 '22 14:09 dvdalilue

I need this feature, how is it now?

jjeejj avatar Apr 10 '23 07:04 jjeejj