From f2b0c08e34a99c1d5bf4e3c3efe531387b302d74 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Sat, 22 Oct 2016 14:54:34 +0530 Subject: [PATCH] logging: print file:line:funcName of the caller of errorIf and fatalIf (#3035) --- cmd/logger.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/logger.go b/cmd/logger.go index ddc91d08b..17ebb4323 100644 --- a/cmd/logger.go +++ b/cmd/logger.go @@ -77,13 +77,27 @@ func stackInfo() string { return strings.Replace(stackBuf.String(), filepath.ToSlash(GOPATH)+"/src/", "", -1) } +// Get file, line, function name of the caller. +func callerLocation() string { + pc, file, line, success := runtime.Caller(2) + if !success { + file = "" + line = 0 + } + shortFile := true // We are only interested in short file form. + callerLoc := funcFromPC(pc, file, line, shortFile) + return callerLoc +} + // errorIf synonymous with fatalIf but doesn't exit on error != nil func errorIf(err error, msg string, data ...interface{}) { if err == nil { return } + location := callerLocation() fields := logrus.Fields{ - "cause": err.Error(), + "location": location, + "cause": err.Error(), } if e, ok := err.(*Error); ok { fields["stack"] = strings.Join(e.Trace(), " ") @@ -97,8 +111,10 @@ func fatalIf(err error, msg string, data ...interface{}) { if err == nil { return } + location := callerLocation() fields := logrus.Fields{ - "cause": err.Error(), + "location": location, + "cause": err.Error(), } if globalTrace { fields["stack"] = "\n" + stackInfo()