From 80100830266e38878d27aa1c882d32445faa4b29 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 14:40:56 +0200 Subject: [PATCH] dendrite/common: Move logrus configuration to common --- .../dendrite/cmd/dendrite-clientapi/main.go | 20 ++------------- .../dendrite/cmd/dendrite-sync-server/main.go | 20 ++------------- .../matrix-org/dendrite/common/log.go | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+), 36 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/common/log.go diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-clientapi/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-clientapi/main.go index 70df0a2d..925c54d6 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-clientapi/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-clientapi/main.go @@ -3,34 +3,18 @@ package main import ( "net/http" "os" - "path/filepath" "golang.org/x/crypto/ed25519" "github.com/matrix-org/dendrite/clientapi/config" "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/clientapi/routing" + "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/roomserver/api" log "github.com/Sirupsen/logrus" - "github.com/matrix-org/dugong" ) -func setupLogging(logDir string) { - _ = os.Mkdir(logDir, os.ModePerm) - log.AddHook(dugong.NewFSHook( - filepath.Join(logDir, "info.log"), - filepath.Join(logDir, "warn.log"), - filepath.Join(logDir, "error.log"), - &log.TextFormatter{ - TimestampFormat: "2006-01-02 15:04:05.000000", - DisableColors: true, - DisableTimestamp: false, - DisableSorting: false, - }, &dugong.DailyRotationSchedule{GZip: true}, - )) -} - func main() { bindAddr := os.Getenv("BIND_ADDRESS") if bindAddr == "" { @@ -38,7 +22,7 @@ func main() { } logDir := os.Getenv("LOG_DIR") if logDir != "" { - setupLogging(logDir) + common.SetupLogging(logDir) } // TODO: Rather than generating a new key on every startup, we should be diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-server/main.go index fe74679a..a8451a45 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-server/main.go @@ -5,8 +5,8 @@ import ( "io/ioutil" "net/http" "os" - "path/filepath" + "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/syncserver/config" "github.com/matrix-org/dendrite/syncserver/consumers" "github.com/matrix-org/dendrite/syncserver/routing" @@ -14,28 +14,12 @@ import ( "github.com/matrix-org/dendrite/syncserver/sync" log "github.com/Sirupsen/logrus" - "github.com/matrix-org/dugong" yaml "gopkg.in/yaml.v2" ) var configPath = flag.String("config", "sync-server-config.yaml", "The path to the config file. For more information, see the config file in this repository.") var bindAddr = flag.String("listen", ":4200", "The port to listen on.") -func setupLogging(logDir string) { - _ = os.Mkdir(logDir, os.ModePerm) - log.AddHook(dugong.NewFSHook( - filepath.Join(logDir, "info.log"), - filepath.Join(logDir, "warn.log"), - filepath.Join(logDir, "error.log"), - &log.TextFormatter{ - TimestampFormat: "2006-01-02 15:04:05.000000", - DisableColors: true, - DisableTimestamp: false, - DisableSorting: false, - }, &dugong.DailyRotationSchedule{GZip: true}, - )) -} - func loadConfig(configPath string) (*config.Sync, error) { contents, err := ioutil.ReadFile(configPath) if err != nil { @@ -65,7 +49,7 @@ func main() { } logDir := os.Getenv("LOG_DIR") if logDir != "" { - setupLogging(logDir) + common.SetupLogging(logDir) } log.Info("sync server config: ", cfg) diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go new file mode 100644 index 00000000..a270a56e --- /dev/null +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -0,0 +1,25 @@ +package common + +import ( + "os" + "path/filepath" + + "github.com/Sirupsen/logrus" + "github.com/matrix-org/dugong" +) + +// SetupLogging configures the logging format and destination(s). +func SetupLogging(logDir string) { + _ = os.Mkdir(logDir, os.ModePerm) + logrus.AddHook(dugong.NewFSHook( + filepath.Join(logDir, "info.log"), + filepath.Join(logDir, "warn.log"), + filepath.Join(logDir, "error.log"), + &logrus.TextFormatter{ + TimestampFormat: "2006-01-02 15:04:05.000000", + DisableColors: true, + DisableTimestamp: false, + DisableSorting: false, + }, &dugong.DailyRotationSchedule{GZip: true}, + )) +} -- GitLab