diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go index dc9386b7..d6d6f36b 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go @@ -17,7 +17,6 @@ package main import ( "net/http" "os" - "path/filepath" "strings" "golang.org/x/crypto/ed25519" @@ -25,27 +24,12 @@ import ( "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}, - )) -} - var ( kafkaURIs = strings.Split(os.Getenv("KAFKA_URIS"), ",") bindAddr = os.Getenv("BIND_ADDRESS") @@ -55,12 +39,10 @@ var ( ) func main() { + common.SetupLogging(logDir) if bindAddr == "" { log.Panic("No BIND_ADDRESS environment variable found.") } - if logDir != "" { - setupLogging(logDir) - } if len(kafkaURIs) == 0 { // the kafka default is :9092 kafkaURIs = []string{"localhost:9092"} diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-api-server/main.go index 84e4de65..750f0203 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-sync-api-server/main.go @@ -19,8 +19,8 @@ import ( "io/ioutil" "net/http" "os" - "path/filepath" + "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/syncapi/config" "github.com/matrix-org/dendrite/syncapi/consumers" "github.com/matrix-org/dendrite/syncapi/routing" @@ -28,28 +28,12 @@ import ( "github.com/matrix-org/dendrite/syncapi/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 { @@ -64,6 +48,8 @@ func loadConfig(configPath string) (*config.Sync, error) { } func main() { + common.SetupLogging(os.Getenv("LOG_DIR")) + flag.Parse() if *configPath == "" { @@ -77,10 +63,6 @@ func main() { if *bindAddr == "" { log.Fatal("--listen must be supplied") } - logDir := os.Getenv("LOG_DIR") - if logDir != "" { - 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..79fee40f --- /dev/null +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -0,0 +1,62 @@ +// 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 ( + "os" + "path/filepath" + + "github.com/Sirupsen/logrus" + "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) { + 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( + filepath.Join(logDir, "info.log"), + filepath.Join(logDir, "warn.log"), + filepath.Join(logDir, "error.log"), + &utcFormatter{ + &logrus.TextFormatter{ + TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", + DisableColors: true, + DisableTimestamp: false, + DisableSorting: false, + }, + }, + &dugong.DailyRotationSchedule{GZip: true}, + )) + } +}