From 80100830266e38878d27aa1c882d32445faa4b29 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 14:40:56 +0200 Subject: [PATCH 1/8] 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}, + )) +} From e8d2d61cc29a66e2236be3cbac9e5a1f419b942e Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 16:17:43 +0200 Subject: [PATCH 2/8] 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) + } } From 254e61f7279c5037e2abed458df99f5dfe9bdfa8 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 16:19:17 +0200 Subject: [PATCH 3/8] common/log: Switch to RFC3339 format with nanoseconds and trailing zeros --- src/github.com/matrix-org/dendrite/common/log.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go index acae9ee7..625ef20a 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -11,7 +11,7 @@ import ( // SetupLogging configures the logging format and destination(s). func SetupLogging(logDir string) { formatter := &logrus.TextFormatter{ - TimestampFormat: "2006-01-02 15:04:05.000000", + TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", DisableColors: true, DisableTimestamp: false, DisableSorting: false, From f65e26bc2a7ce3e1cd9305d6b3d9d10b8dbe95be Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 16:26:34 +0200 Subject: [PATCH 4/8] cmd: Configure logging before any log messages --- .../matrix-org/dendrite/cmd/dendrite-clientapi/main.go | 3 ++- .../matrix-org/dendrite/cmd/dendrite-sync-server/main.go | 3 ++- 2 files changed, 4 insertions(+), 2 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 0346785e..0fa0e6b6 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 @@ -16,11 +16,12 @@ import ( ) func main() { + common.SetupLogging(os.Getenv("LOG_DIR")) + bindAddr := os.Getenv("BIND_ADDRESS") if bindAddr == "" { log.Panic("No BIND_ADDRESS environment variable found.") } - 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 3dc6b6cb..a84dd848 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 @@ -34,6 +34,8 @@ func loadConfig(configPath string) (*config.Sync, error) { } func main() { + common.SetupLogging(os.Getenv("LOG_DIR")) + flag.Parse() if *configPath == "" { @@ -47,7 +49,6 @@ func main() { if *bindAddr == "" { log.Fatal("--listen must be supplied") } - common.SetupLogging(os.Getenv("LOG_DIR")) log.Info("sync server config: ", cfg) From be47984c430b866377f1e86ff2354e8e88215d69 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 20 Apr 2017 17:15:30 +0200 Subject: [PATCH 5/8] common/log: Always output timestamps as UTC --- .../matrix-org/dendrite/common/log.go | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go index 625ef20a..6bed9c9a 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -8,13 +8,24 @@ import ( "github.com/matrix-org/dugong" ) +type utcFormatter struct { + logrus.Formatter +} + +func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) { + entry.Time = entry.Time.UTC() + return f.Formatter.Format(entry) +} + // SetupLogging configures the logging format and destination(s). func SetupLogging(logDir string) { - formatter := &logrus.TextFormatter{ - TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", - DisableColors: true, - DisableTimestamp: false, - DisableSorting: false, + formatter := &utcFormatter{ + &logrus.TextFormatter{ + TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", + DisableColors: true, + DisableTimestamp: false, + DisableSorting: false, + }, } if logDir != "" { _ = os.Mkdir(logDir, os.ModePerm) From 9b7bf8cba8fc42ad9096f2dff483e55de85502e2 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Fri, 21 Apr 2017 01:45:18 +0200 Subject: [PATCH 6/8] common/log: Add license header --- src/github.com/matrix-org/dendrite/common/log.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go index 6bed9c9a..e6d79458 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -1,3 +1,17 @@ +// Copyright 2017 Vector Creations Ltd +// +// 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 common import ( From 27beb8a2b390985897923ca612c950a12f5554a6 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Fri, 21 Apr 2017 02:06:06 +0200 Subject: [PATCH 7/8] common/log: Re-colorize terminal log output --- .../matrix-org/dendrite/common/log.go | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go index e6d79458..b8ea9547 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -33,24 +33,31 @@ func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) { // SetupLogging configures the logging format and destination(s). func SetupLogging(logDir string) { - formatter := &utcFormatter{ - &logrus.TextFormatter{ - TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", - 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, + &utcFormatter{ + &logrus.TextFormatter{ + TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", + DisableColors: true, + DisableTimestamp: false, + DisableSorting: false, + }, + }, &dugong.DailyRotationSchedule{GZip: true}, )) } else { - logrus.SetFormatter(formatter) + logrus.SetFormatter(&utcFormatter{ + &logrus.TextFormatter{ + TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", + FullTimestamp: true, + DisableColors: false, + DisableTimestamp: false, + DisableSorting: false, + }, + }) } } From 9b7defd37506e8b592b5c25cf1b1b8c15df4520a Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Fri, 21 Apr 2017 02:10:54 +0200 Subject: [PATCH 8/8] common/log: Always log to stderr --- .../matrix-org/dendrite/common/log.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go index b8ea9547..79fee40f 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -33,6 +33,15 @@ func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) { // SetupLogging configures the logging format and destination(s). func SetupLogging(logDir string) { + logrus.SetFormatter(&utcFormatter{ + &logrus.TextFormatter{ + TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", + FullTimestamp: true, + DisableColors: false, + DisableTimestamp: false, + DisableSorting: false, + }, + }) if logDir != "" { _ = os.Mkdir(logDir, os.ModePerm) logrus.AddHook(dugong.NewFSHook( @@ -49,15 +58,5 @@ func SetupLogging(logDir string) { }, &dugong.DailyRotationSchedule{GZip: true}, )) - } else { - logrus.SetFormatter(&utcFormatter{ - &logrus.TextFormatter{ - TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", - FullTimestamp: true, - DisableColors: false, - DisableTimestamp: false, - DisableSorting: false, - }, - }) } }