From e2d73f9205c03139b1f0b7e7a33043d478c747ce Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 28 Nov 2022 13:14:59 +0100 Subject: [PATCH] general: remove custom logger It seems like it does not really make logs appear more readable unfortunately, especially in docker environment. We may want to switch to a different structured logging instead. See https://github.com/matrix-org/waterfall/issues/50 --- pkg/logger.go | 74 ---------------------------------------------- pkg/main.go | 3 +- scripts/profile.sh | 2 +- scripts/run.sh | 2 +- 4 files changed, 3 insertions(+), 78 deletions(-) delete mode 100644 pkg/logger.go diff --git a/pkg/logger.go b/pkg/logger.go deleted file mode 100644 index 9d725ae..0000000 --- a/pkg/logger.go +++ /dev/null @@ -1,74 +0,0 @@ -/* -Copyright 2022 The Matrix.org Foundation C.I.C. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "fmt" - - "github.com/sirupsen/logrus" -) - -func InitLogging(logTime bool) { - formatter := new(CustomTextFormatter) - formatter.logTime = logTime - logrus.SetFormatter(formatter) -} - -type CustomTextFormatter struct { - logrus.TextFormatter - logTime bool -} - -func (f *CustomTextFormatter) Format(entry *logrus.Entry) ([]byte, error) { - // TODO: Use colors and make it pretty - level := entry.Level - timestamp := entry.Time.Format("2006-01-02 15:04:05") - confID := entry.Data["conf_id"] - userID := entry.Data["user_id"] - - logLine := fmt.Sprintf("%s|", level) - - if f.logTime { - logLine += fmt.Sprintf("%s|", timestamp) - } - - if confID != nil { - logLine += fmt.Sprintf("%v|", confID) - } - - if userID != nil { - logLine += fmt.Sprintf("%v|", userID) - } - - logLine += fmt.Sprintf(" %v ", entry.Message) - - fields := "" - - for key, value := range entry.Data { - if key != "conf_id" && key != "user_id" { - fields += fmt.Sprintf("%v=%v ", key, value) - } - } - - if fields != "" { - logLine += fmt.Sprintf("| %s", fields) - } - - logLine += "\n" - - return []byte(logLine), nil -} diff --git a/pkg/main.go b/pkg/main.go index 6e09354..1fe4531 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -31,7 +31,6 @@ import ( func main() { // Parse command line flags. var ( - logTime = flag.Bool("logTime", false, "whether or not to print time and date in logs") configFilePath = flag.String("config", "config.yaml", "configuration file path") cpuProfile = flag.String("cpuProfile", "", "write CPU profile to `file`") memProfile = flag.String("memProfile", "", "write memory profile to `file`") @@ -39,7 +38,7 @@ func main() { flag.Parse() // Initialize logging subsystem (formatting, global logging framework etc). - InitLogging(*logTime) + logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) // Define functions that are called before exiting. // This is useful to stop the profiler if it's enabled. diff --git a/scripts/profile.sh b/scripts/profile.sh index 0042a33..7bc0ee9 100755 --- a/scripts/profile.sh +++ b/scripts/profile.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -go run ./pkg/*.go --cpuProfile cpuProfile.pprof --memProfile memProfile.pprof --logTime +go run ./pkg/*.go --cpuProfile cpuProfile.pprof --memProfile memProfile.pprof diff --git a/scripts/run.sh b/scripts/run.sh index 16e9b1f..6381271 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -go run ./pkg --logTime +go run ./pkg