diff options
Diffstat (limited to 'vendor/github.com/matterbridge/logrus-prefixed-formatter')
-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("^\\[(.*?)\\]") |