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 8794107fc..07f815b89 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 ( "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -37,12 +36,11 @@ import ( ) var ( - logDir = os.Getenv("LOG_DIR") configPath = flag.String("config", "dendrite.yaml", "The path to the config file, For more information see the config file in this repository") ) func main() { - common.SetupLogging(logDir) + common.SetupStdLogging() flag.Parse() @@ -51,6 +49,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteClientAPI") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go index 53587ee20..4e199f92e 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go @@ -17,7 +17,6 @@ package main import ( "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -33,12 +32,11 @@ import ( ) var ( - logDir = os.Getenv("LOG_DIR") configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") ) func main() { - common.SetupLogging(logDir) + common.SetupStdLogging() flag.Parse() @@ -50,6 +48,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteFederationAPI") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-sender-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-sender-server/main.go index 656b6adaf..d55c16e71 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-sender-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-sender-server/main.go @@ -17,7 +17,6 @@ package main import ( "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/common" @@ -35,7 +34,7 @@ import ( var configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") func main() { - common.SetupLogging(os.Getenv("LOG_DIR")) + common.SetupStdLogging() flag.Parse() @@ -47,6 +46,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteFederationSender") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-media-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-media-api-server/main.go index bc16dee79..050edf511 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-media-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-media-api-server/main.go @@ -17,7 +17,6 @@ package main import ( "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" @@ -31,12 +30,11 @@ import ( ) var ( - logDir = os.Getenv("LOG_DIR") configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") ) func main() { - common.SetupLogging(logDir) + common.SetupStdLogging() flag.Parse() @@ -48,6 +46,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteMediaAPI") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go index 9ecfd60ab..27b98c200 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go @@ -19,7 +19,6 @@ import ( "database/sql" "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -63,7 +62,6 @@ import ( ) var ( - logDir = os.Getenv("LOG_DIR") configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server") httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server") @@ -72,7 +70,7 @@ var ( ) func main() { - common.SetupLogging(logDir) + common.SetupStdLogging() flag.Parse() @@ -84,6 +82,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteMonolith") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go index 24aae0dac..953bd2c20 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go @@ -17,7 +17,6 @@ package main import ( "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" @@ -35,7 +34,7 @@ import ( var configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") func main() { - common.SetupLogging(os.Getenv("LOG_DIR")) + common.SetupStdLogging() flag.Parse() @@ -47,6 +46,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendritePublicRoomsAPI") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go index 06773972a..4713046c4 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go @@ -18,7 +18,6 @@ import ( "flag" "net/http" _ "net/http/pprof" - "os" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" @@ -32,12 +31,11 @@ import ( ) var ( - logDir = os.Getenv("LOG_DIR") configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") ) func main() { - common.SetupLogging(logDir) + common.SetupStdLogging() flag.Parse() @@ -49,6 +47,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteRoomServer") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") 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 16ae228b2..4b4cf8572 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 @@ -18,7 +18,6 @@ import ( "context" "flag" "net/http" - "os" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -39,7 +38,7 @@ import ( var configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") func main() { - common.SetupLogging(os.Getenv("LOG_DIR")) + common.SetupStdLogging() flag.Parse() @@ -51,6 +50,8 @@ func main() { log.Fatalf("Invalid config file: %s", err) } + common.SetupFileLogging(string(cfg.Logging.FPath), cfg.Derived.LogLevel) + closer, err := cfg.SetupTracing("DendriteSyncAPI") if err != nil { log.WithError(err).Fatalf("Failed to start tracer") diff --git a/src/github.com/matrix-org/dendrite/common/log.go b/src/github.com/matrix-org/dendrite/common/log.go index fbfa34783..0aad8e4b6 100644 --- a/src/github.com/matrix-org/dendrite/common/log.go +++ b/src/github.com/matrix-org/dendrite/common/log.go @@ -16,7 +16,7 @@ package common import ( "os" - "path/filepath" + "path" "github.com/matrix-org/dugong" "github.com/sirupsen/logrus" @@ -31,8 +31,8 @@ func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) { return f.Formatter.Format(entry) } -// SetupLogging configures the logging format and destination(s). -func SetupLogging(logDir string) { +// SetupStdLogging configures the logging format to standard output. Typically, it is called when the config is not yet loaded. +func SetupStdLogging() { logrus.SetFormatter(&utcFormatter{ &logrus.TextFormatter{ TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", @@ -42,12 +42,15 @@ func SetupLogging(logDir string) { DisableSorting: false, }, }) - if logDir != "" { - _ = os.Mkdir(logDir, os.ModePerm) +} + +// SetupFileLogging configures the logging format to a file. +func SetupFileLogging(logFile string, logLevel logrus.Level) { + logrus.SetLevel(logLevel) + if logFile != "" { + _ = os.MkdirAll(path.Dir(logFile), os.ModePerm) logrus.AddHook(dugong.NewFSHook( - filepath.Join(logDir, "info.log"), - filepath.Join(logDir, "warn.log"), - filepath.Join(logDir, "error.log"), + logFile, &utcFormatter{ &logrus.TextFormatter{ TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",