Merge pull request #75 from matrix-org/rob/common-logging

Improve logging configuration
This commit is contained in:
Robert Swain 2017-04-21 17:48:17 +02:00 committed by GitHub
commit a1ce351d36
3 changed files with 67 additions and 41 deletions

View file

@ -17,7 +17,6 @@ package main
import ( import (
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
"golang.org/x/crypto/ed25519" "golang.org/x/crypto/ed25519"
@ -25,27 +24,12 @@ import (
"github.com/matrix-org/dendrite/clientapi/config" "github.com/matrix-org/dendrite/clientapi/config"
"github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/clientapi/producers"
"github.com/matrix-org/dendrite/clientapi/routing" "github.com/matrix-org/dendrite/clientapi/routing"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
log "github.com/Sirupsen/logrus" 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 ( var (
kafkaURIs = strings.Split(os.Getenv("KAFKA_URIS"), ",") kafkaURIs = strings.Split(os.Getenv("KAFKA_URIS"), ",")
bindAddr = os.Getenv("BIND_ADDRESS") bindAddr = os.Getenv("BIND_ADDRESS")
@ -55,12 +39,10 @@ var (
) )
func main() { func main() {
common.SetupLogging(logDir)
if bindAddr == "" { if bindAddr == "" {
log.Panic("No BIND_ADDRESS environment variable found.") log.Panic("No BIND_ADDRESS environment variable found.")
} }
if logDir != "" {
setupLogging(logDir)
}
if len(kafkaURIs) == 0 { if len(kafkaURIs) == 0 {
// the kafka default is :9092 // the kafka default is :9092
kafkaURIs = []string{"localhost:9092"} kafkaURIs = []string{"localhost:9092"}

View file

@ -19,8 +19,8 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/syncapi/config" "github.com/matrix-org/dendrite/syncapi/config"
"github.com/matrix-org/dendrite/syncapi/consumers" "github.com/matrix-org/dendrite/syncapi/consumers"
"github.com/matrix-org/dendrite/syncapi/routing" "github.com/matrix-org/dendrite/syncapi/routing"
@ -28,28 +28,12 @@ import (
"github.com/matrix-org/dendrite/syncapi/sync" "github.com/matrix-org/dendrite/syncapi/sync"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dugong"
yaml "gopkg.in/yaml.v2" 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 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.") 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) { func loadConfig(configPath string) (*config.Sync, error) {
contents, err := ioutil.ReadFile(configPath) contents, err := ioutil.ReadFile(configPath)
if err != nil { if err != nil {
@ -64,6 +48,8 @@ func loadConfig(configPath string) (*config.Sync, error) {
} }
func main() { func main() {
common.SetupLogging(os.Getenv("LOG_DIR"))
flag.Parse() flag.Parse()
if *configPath == "" { if *configPath == "" {
@ -77,10 +63,6 @@ func main() {
if *bindAddr == "" { if *bindAddr == "" {
log.Fatal("--listen must be supplied") log.Fatal("--listen must be supplied")
} }
logDir := os.Getenv("LOG_DIR")
if logDir != "" {
setupLogging(logDir)
}
log.Info("sync server config: ", cfg) log.Info("sync server config: ", cfg)

View file

@ -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},
))
}
}