diff options
author | Wim <wim@42.be> | 2020-05-24 13:58:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-24 13:58:15 +0200 |
commit | 9440b9e3138805945214aa4d86368bb40da07cc2 (patch) | |
tree | 611fe471c477407c58faea15828bc5d31284883d /vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go | |
parent | 393f9e998b1b40aa59d3fb8794c3a73da38c3fb7 (diff) | |
download | matterbridge-msglm-9440b9e3138805945214aa4d86368bb40da07cc2.tar.gz matterbridge-msglm-9440b9e3138805945214aa4d86368bb40da07cc2.tar.bz2 matterbridge-msglm-9440b9e3138805945214aa4d86368bb40da07cc2.zip |
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.
Diffstat (limited to 'vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go')
-rw-r--r-- | vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go b/vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go index 7cb807da..434c2982 100644 --- a/vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go +++ b/vendor/github.com/matterbridge/logrus-prefixed-formatter/formatter.go @@ -6,6 +6,7 @@ import ( "io" "os" "regexp" + "runtime" "sort" "strings" "sync" @@ -121,6 +122,14 @@ type TextFormatter struct { // Whether the logger's out is to a terminal. isTerminal bool + // CallerPrettyfier can be set by the user to modify the content + // of the function and file keys in the data when ReportCaller is + // activated. If any of the returned value is the empty string the + // corresponding key will be removed from fields. + CallerPrettyfier func(*runtime.Frame) (function string, file string) + + CallerFormatter func(function, file string) string + sync.Once } @@ -217,6 +226,23 @@ func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) { if entry.Message != "" { f.appendKeyValue(b, "msg", entry.Message, lastKeyIdx >= 0) } + + if entry.HasCaller() { + var funcVal, fileVal string + if f.CallerPrettyfier != nil { + funcVal, fileVal = f.CallerPrettyfier(entry.Caller) + } else { + funcVal, fileVal = extractCallerInfo(entry.Caller) + } + + if funcVal != "" { + f.appendKeyValue(b, "func", funcVal, true) + } + if fileVal != "" { + f.appendKeyValue(b, "file", fileVal, true) + } + } + for i, key := range keys { f.appendKeyValue(b, key, entry.Data[key], lastKeyIdx != i) } @@ -276,7 +302,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys prefixFormat := "%s" if f.PrefixPadding != 0 { - prefixFormat = fmt.Sprintf("%%-%ds", adjustedPrefixPadding) + prefixFormat = fmt.Sprintf("%%-%ds%%s", adjustedPrefixPadding) } messageFormat := "%s" @@ -284,8 +310,24 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys messageFormat = fmt.Sprintf("%%-%ds", f.SpacePadding) } + caller := "" + if entry.HasCaller() { + var funcVal, fileVal string + if f.CallerPrettyfier != nil { + funcVal, fileVal = f.CallerPrettyfier(entry.Caller) + } else { + funcVal, fileVal = extractCallerInfo(entry.Caller) + } + + if f.CallerFormatter != nil { + caller = f.CallerFormatter(funcVal, fileVal) + } else { + caller = fmt.Sprintf(" (%s: %s)", fileVal, funcVal) + } + } + if f.DisableTimestamp { - fmt.Fprintf(b, "%s"+prefixFormat+" "+messageFormat, level, prefix, message) + fmt.Fprintf(b, "%s"+prefixFormat+" "+messageFormat, level, prefix, caller, message) } else { var timestamp string if !f.FullTimestamp { @@ -293,7 +335,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys } else { timestamp = fmt.Sprintf("[%s]", entry.Time.Format(timestampFormat)) } - fmt.Fprintf(b, "%s %s"+prefixFormat+" "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message) + fmt.Fprintf(b, "%s %s"+prefixFormat+" "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, caller, message) } for _, k := range keys { if k != "prefix" { @@ -318,6 +360,12 @@ func (f *TextFormatter) needsQuoting(text string) bool { return false } +func extractCallerInfo(caller *runtime.Frame) (string, string) { + funcVal := caller.Function + fileVal := fmt.Sprintf("%s:%d", caller.File, caller.Line) + return funcVal, fileVal +} + func extractPrefix(msg string) (string, string) { prefix := "" regex := regexp.MustCompile("^\\[(.*?)\\]") |