diff options
author | Richard Burke <rich.g.burke@gmail.com> | 2019-03-16 16:28:45 +0000 |
---|---|---|
committer | Richard Burke <rich.g.burke@gmail.com> | 2019-03-16 16:28:45 +0000 |
commit | c80d695470189e3d621156b9ccfafefa1f361736 (patch) | |
tree | c13b37d2ac5cca16150a04a78a13cb4168a6807d | |
parent | 416bfbb4cf9c2cf2e75710ae1ee7c401b162ab0c (diff) |
Fix file names in logs
-rw-r--r-- | cmd/grv/log.go | 37 |
1 files changed, 9 insertions, 28 deletions
diff --git a/cmd/grv/log.go b/cmd/grv/log.go index b21c5ac..10b0f9f 100644 --- a/cmd/grv/log.go +++ b/cmd/grv/log.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "os" "path" - "runtime" "strings" log "github.com/Sirupsen/logrus" @@ -19,35 +18,17 @@ const ( var logFile string -type fileHook struct{} - -func (hook fileHook) Fire(entry *log.Entry) (err error) { - pc := make([]uintptr, 5) - cnt := runtime.Callers(6, pc) - - for i := 0; i < cnt; i++ { - fu := runtime.FuncForPC(pc[i] - 1) - name := fu.Name() - - if !strings.Contains(name, logLogrusRepo) { - file, line := fu.FileLine(pc[i] - 1) - entry.Data["file"] = fmt.Sprintf("%v:%v", path.Base(file), line) - break - } - } - - return -} - -func (hook fileHook) Levels() []log.Level { - return log.AllLevels -} - type logFormatter struct{} func (formatter logFormatter) Format(entry *log.Entry) ([]byte, error) { var buffer bytes.Buffer - file, _ := entry.Data["file"].(string) + var file string + + if entry.HasCaller() { + file = fmt.Sprintf("%v:%v", path.Base(entry.Caller.File), entry.Caller.Line) + } else { + file = "Unknown" + } formatter.formatBracketEntry(&buffer, entry.Time.Format(logFileDateFormat)) formatter.formatBracketEntry(&buffer, strings.ToUpper(entry.Level.String())) @@ -113,7 +94,7 @@ func InitialiseLogging(logLevel, logFilePath string) { log.SetOutput(file) - log.SetFormatter(logFormatter{}) + log.SetReportCaller(true) - log.AddHook(fileHook{}) + log.SetFormatter(logFormatter{}) } |