4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-06-26 08:39:24 +00:00

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.
This commit is contained in:
Wim
2020-05-24 13:58:15 +02:00
committed by GitHub
parent 393f9e998b
commit 9440b9e313
5 changed files with 75 additions and 12 deletions

View File

@ -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("^\\[(.*?)\\]")

2
vendor/modules.txt vendored
View File

@ -107,7 +107,7 @@ github.com/matterbridge/go-xmpp
github.com/matterbridge/gomatrix
# github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18
github.com/matterbridge/gozulipbot
# github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61
# github.com/matterbridge/logrus-prefixed-formatter v0.5.3-0.20200523233437-d971309a77ba
github.com/matterbridge/logrus-prefixed-formatter
# github.com/mattermost/mattermost-server v5.5.0+incompatible
github.com/mattermost/mattermost-server/mlog