From 9440b9e3138805945214aa4d86368bb40da07cc2 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 24 May 2020 13:58:15 +0200 Subject: Increase debug logging with function,file and linenumber (#1147) Show the function name,file and linenumber like this [0000] INFO main: [setupLogger:matterbridge.go:100] Enabling debug logging. [0000] INFO main: [main:matterbridge.go:46] Running version 1.17.5-dev Only enable this for debug as this adds some overhead. --- matterbridge.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'matterbridge.go') diff --git a/matterbridge.go b/matterbridge.go index da0df103..24caa5d9 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "runtime" "strings" "github.com/42wim/matterbridge/bridge/config" @@ -67,17 +68,31 @@ func setupLogger() *logrus.Logger { Formatter: &prefixed.TextFormatter{ PrefixPadding: 13, DisableColors: true, - FullTimestamp: true, }, Level: logrus.InfoLevel, } if *flagDebug || os.Getenv("DEBUG") == "1" { + logger.SetReportCaller(true) logger.Formatter = &prefixed.TextFormatter{ - PrefixPadding: 13, - DisableColors: true, - FullTimestamp: false, - ForceFormatting: true, + PrefixPadding: 13, + DisableColors: true, + FullTimestamp: false, + + CallerFormatter: func(function, file string) string { + return fmt.Sprintf(" [%s:%s]", function, file) + }, + CallerPrettyfier: func(f *runtime.Frame) (string, string) { + sp := strings.SplitAfter(f.File, "/matterbridge/") + filename := f.File + if len(sp) > 1 { + filename = sp[1] + } + s := strings.Split(f.Function, ".") + funcName := s[len(s)-1] + return funcName, fmt.Sprintf("%s:%d", filename, f.Line) + }, } + logger.Level = logrus.DebugLevel logger.WithFields(logrus.Fields{"prefix": "main"}).Info("Enabling debug logging.") } -- cgit v1.2.3