From e8d2d61cc29a66e2236be3cbac9e5a1f419b942e Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 16:17:43 +0200 Subject: [PATCH] cmd: common/log: Always configure logging When LOG_DIR or so is not specified, just configure the formatter. --- .../dendrite/cmd/dendrite-clientapi/main.go | 5 +--- .../dendrite/cmd/dendrite-sync-server/main.go | 5 +--- .../matrix-org/dendrite/common/log.go | 30 +++++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) 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 925c54d6..0346785e 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 @@ -20,10 +20,7 @@ func main() { if bindAddr == "" { log.Panic("No BIND_ADDRESS environment variable found.") } - logDir := os.Getenv("LOG_DIR") - if logDir != "" { - common.SetupLogging(logDir) - } + common.SetupLogging(os.Getenv("LOG_DIR")) // TODO: Rather than generating a new key on every startup, we should be // reading a PEM formatted file instead. 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 a8451a45..3dc6b6cb 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 @@ -47,10 +47,7 @@ func main() { if *bindAddr == "" { log.Fatal("--listen must be supplied") } - logDir := os.Getenv("LOG_DIR") - if logDir != "" { - common.SetupLogging(logDir) - } + common.SetupLogging(os.Getenv("LOG_DIR")) 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 index a270a56e..acae9ee7 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -10,16 +10,22 @@ import ( // 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}, - )) + formatter := &logrus.TextFormatter{ + TimestampFormat: "2006-01-02 15:04:05.000000", + DisableColors: true, + DisableTimestamp: false, + DisableSorting: false, + } + if logDir != "" { + _ = os.Mkdir(logDir, os.ModePerm) + logrus.AddHook(dugong.NewFSHook( + filepath.Join(logDir, "info.log"), + filepath.Join(logDir, "warn.log"), + filepath.Join(logDir, "error.log"), + formatter, + &dugong.DailyRotationSchedule{GZip: true}, + )) + } else { + logrus.SetFormatter(formatter) + } }