From b0a12e437febc9833be2e6c614d5e2cb299d2a1e Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 23 Jul 2019 11:01:26 +0100 Subject: [PATCH] Use simpler, twoline logger --- clientapi/clientapi.go | 1 - common/log.go | 111 ++++------------------------------------- 2 files changed, 9 insertions(+), 103 deletions(-) diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index 293700d28..5b6e21c80 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -44,7 +44,6 @@ func SetupClientAPIComponent( asAPI appserviceAPI.AppServiceQueryAPI, transactionsCache *transactions.Cache, ) { - roomserverProducer := producers.NewRoomserverProducer(inputAPI) typingProducer := producers.NewTypingServerProducer(typingInputAPI) diff --git a/common/log.go b/common/log.go index 902a76b02..f9ed84edb 100644 --- a/common/log.go +++ b/common/log.go @@ -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 {