From 43ccc2098767facdc0c8e16b443837fffc2104d8 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 23 Jul 2019 10:56:23 +0100 Subject: [PATCH] Multiple different formats --- clientapi/clientapi.go | 1 + common/log.go | 119 ++++++++++++++++++++++++++++++++++++++++- go.mod | 8 +-- go.sum | 9 ++++ 4 files changed, 133 insertions(+), 4 deletions(-) diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index 5b6e21c80..293700d28 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -44,6 +44,7 @@ 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 89a705822..902a76b02 100644 --- a/common/log.go +++ b/common/log.go @@ -15,9 +15,12 @@ package common import ( + "fmt" "os" "path" "path/filepath" + "runtime" + "strings" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dugong" @@ -54,15 +57,127 @@ 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 +} + +func callerPrettyfier(f *runtime.Frame) (string, string) { + 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)) + + // 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) + } + + // 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 +} + // SetupStdLogging configures the logging format to standard output. Typically, it is called when the config is not yet loaded. func SetupStdLogging() { + logrus.SetReportCaller(true) logrus.SetFormatter(&utcFormatter{ &logrus.TextFormatter{ TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", FullTimestamp: true, DisableColors: false, DisableTimestamp: false, - DisableSorting: false, + QuoteEmptyFields: true, + CallerPrettyfier: callerPrettyfier, }, }) } @@ -71,6 +186,7 @@ func SetupStdLogging() { // If something fails here it means that the logging was improperly configured, // so we just exit with the error func SetupHookLogging(hooks []config.LogrusHook, componentName string) { + logrus.SetReportCaller(true) for _, hook := range hooks { // Check we received a proper logging level @@ -126,6 +242,7 @@ func setupFileHook(hook config.LogrusHook, level logrus.Level, componentName str DisableColors: true, DisableTimestamp: false, DisableSorting: false, + QuoteEmptyFields: true, }, }, &dugong.DailyRotationSchedule{GZip: true}, diff --git a/go.mod b/go.mod index 3b4b736a4..4e5123730 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/jaegertracing/jaeger-client-go v0.0.0-20170921145708-3ad49a1d839b github.com/jaegertracing/jaeger-lib v0.0.0-20170920222118-21a3da6d66fe github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6 + github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/lib/pq v0.0.0-20170918175043-23da1db4f16d github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5 github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 @@ -40,8 +41,9 @@ require ( github.com/prometheus/common v0.0.0-20170108231212-dd2f054febf4 github.com/prometheus/procfs v0.0.0-20170128160123-1878d9fbb537 github.com/rcrowley/go-metrics v0.0.0-20161128210544-1f30fe9094a5 - github.com/sirupsen/logrus v1.3.0 - github.com/stretchr/testify v1.2.2 + github.com/sirupsen/logrus v1.4.2 + github.com/stretchr/objx v0.2.0 // indirect + github.com/stretchr/testify v1.3.0 github.com/tidwall/gjson v1.1.5 github.com/tidwall/match v1.0.1 github.com/tidwall/sjson v1.0.3 @@ -54,7 +56,7 @@ require ( go.uber.org/zap v1.7.1 golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 - golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 + golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 gopkg.in/Shopify/sarama.v1 v1.11.0 gopkg.in/airbrake/gobrake.v2 v2.0.9 gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20170727041045-23bcc3c4eae3 diff --git a/go.sum b/go.sum index 8026640b5..5fd3dc5b4 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,7 @@ github.com/jaegertracing/jaeger-lib v0.0.0-20170920222118-21a3da6d66fe/go.mod h1 github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6 h1:KAZ1BW2TCmT6PRihDPpocIy1QTtsAsrx6TneU/4+CMg= github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -90,9 +91,14 @@ github.com/sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2 h1:+8J/sCAVv2Y9Ct1 github.com/sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v0.0.0-20170809224252-890a5c3458b4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/tidwall/gjson v1.0.2 h1:5BsM7kyEAHAUGEGDkEKO9Mdyiuw6QQ6TSDdarP0Nnmk= github.com/tidwall/gjson v1.0.2/go.mod h1:c/nTNbUr0E0OrXEhq1pwa8iEgc2DOt4ZZqAt1HtCkPA= github.com/tidwall/gjson v1.1.5 h1:QysILxBeUEY3GTLA0fQVgkQG1zme8NxGvhh2SSqWNwI= @@ -128,6 +134,9 @@ golang.org/x/sys v0.0.0-20171012164349-43eea11bc926 h1:PY6OU86NqbyZiOzaPnDw6oOjA golang.org/x/sys v0.0.0-20171012164349-43eea11bc926/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 h1:LepdCS8Gf/MVejFIt8lsiexZATdoGVyp5bcyS+rYoUI= +golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/Shopify/sarama.v1 v1.11.0 h1:/3kaCyeYaPbr59IBjeqhIcUOB1vXlIVqXAYa5g5C5F0= gopkg.in/Shopify/sarama.v1 v1.11.0/go.mod h1:AxnvoaevB2nBjNK17cG61A3LleFcWFwVBHBt+cot4Oc= gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=