logrus
logrus copied to clipboard
add caller skip
can help me skip some stack
Thanks. I also need this feature. No need to implement
sirupsen/logrus/exported.go need SetCallerSkip funcation
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
})```
I am interested in this as well. Anything blocking it moving forward?
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) }
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{})
- ...
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.
I need this feature, how is it now?