Use simpler, twoline logger

This commit is contained in:
Andrew Morgan 2019-07-23 11:01:26 +01:00
parent 43ccc20987
commit b0a12e437f
2 changed files with 9 additions and 103 deletions

View file

@ -44,7 +44,6 @@ func SetupClientAPIComponent(
asAPI appserviceAPI.AppServiceQueryAPI,
transactionsCache *transactions.Cache,
) {
roomserverProducer := producers.NewRoomserverProducer(inputAPI)
typingProducer := producers.NewTypingServerProducer(typingInputAPI)

View file

@ -57,114 +57,22 @@ func (h *logLevelHook) Levels() []logrus.Level {
return levels
}
var (
maxFilenameLength = 30
maxFuncnameLength = 20
)
// callerPrettyfier is a function that given a runtime.Frame object, will
// extract the calling function's name and file, and return them with some
// formatting to cut them down slightly (to make logs nicer to read)
func callerPrettyfier2(f *runtime.Frame) (string, string) {
// Get just the name of the calling function
funcsplit := strings.Split(f.Function, ".")
funcname := funcsplit[len(funcsplit)-1]
// If in debug mode, print the full path to the calling file
var filename string
if false { // debug
filename = fmt.Sprintf("[%s:%d]", f.File, f.Line)
} else {
// Otherwise compress and truncate it
compressedFilepath := truncateDirectoryNames(f.File)
// Add the line number to the end
filename = fmt.Sprintf("%s:%d", compressedFilepath, f.Line)
// Truncate/pad the resulting string to fit it into a certain number of
// characters
filename = padWithLength(filename, maxFilenameLength, true)
}
// Truncate/pad filename lengths if necessary
funcname = padWithLength(funcname, maxFuncnameLength, false)
return funcname, " " + filename
}
// extract the calling function's name and file, and return them in a nicely
// formatted way
func callerPrettyfier(f *runtime.Frame) (string, string) {
// Retrieve just the function name
s := strings.Split(f.Function, ".")
funcname := s[len(s)-1]
return funcname + "\n\t", fmt.Sprintf(" [%s:%d]", f.File, f.Line)
}
// truncateDirectoryNames is a function that takes in a filepath, then returns
// the same path but with all directory names truncated to one character.
// So ex. /home/user/music/mysong.mp3 -> /h/u/m/mysong.mp3
// Intended for use only with absolute paths
func truncateDirectoryNames(filepath string) string {
// Split the filepath into a slice
pathComponents := strings.Split(filepath, "/")[1:] // cut off " " generated by first "/"
// Create a slice for holding the 1 char dir names
truncatedComponents := make([]string, 0, len(pathComponents))
// Append a newline + tab to it to move the actual log content to its own line
funcname += "\n\t"
// Iterate through each directory name and get the first character of each
for _, directory := range pathComponents[:len(pathComponents)-1] {
// Save each character
character := string([]rune(directory)[0])
truncatedComponents = append(truncatedComponents, character)
}
// Surround the filepath in brackets and append line number so IDEs can quickly
// navigate
filename := fmt.Sprintf(" [%s:%d]", f.File, f.Line)
// Add the filename to the end of the array
truncatedComponents = append(truncatedComponents, pathComponents[len(pathComponents)-1])
// Join everything by `/` and return
return "/" + strings.Join(truncatedComponents, "/")
}
func padWithLength(content string, length int, reverseTruncate bool) (ret string) {
fmt.Printf("Got: %s\n", content)
// Create an empty slice with the length of the content
slice := make([]string, length, length)
for index := range slice {
slice[index] = " "
}
// Insert each character from `content` into the string slice, prepending and
// appending with square brackets
// TODO: Insert ellipses
slice[0] = "["
var setEnd = false
if reverseTruncate {
if len(content) >= length {
slice[length-1] = "]"
}
for i := length - 2; i > 0; i-- {
if i < len(content) {
slice[i] = string([]rune(content)[len(content)-(length-i)+1])
} else if i == len(content) {
slice[i] = "]"
}
}
} else {
for i := 1; i < length; i++ {
if i <= len(content) {
slice[i] = string([]rune(content)[i-1])
} else if i == len(content)+1 {
slice[i] = "]"
setEnd = true
}
if i == length-1 && !setEnd {
slice[i] = "]"
}
}
}
// Convert slice to a string
for _, char := range slice {
ret += char
}
return
return funcname, filename
}
// SetupStdLogging configures the logging format to standard output. Typically, it is called when the config is not yet loaded.
@ -188,7 +96,6 @@ func SetupStdLogging() {
func SetupHookLogging(hooks []config.LogrusHook, componentName string) {
logrus.SetReportCaller(true)
for _, hook := range hooks {
// Check we received a proper logging level
level, err := logrus.ParseLevel(hook.Level)
if err != nil {