mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-11 16:13:10 -06:00
zerolog - first apps
This commit is contained in:
parent
2b34f88fde
commit
e064ee5058
|
|
@ -29,9 +29,10 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/term"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup"
|
||||
|
|
@ -73,6 +74,8 @@ var cl = http.Client{
|
|||
Transport: http.DefaultTransport,
|
||||
}
|
||||
|
||||
var logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||
|
||||
func main() {
|
||||
name := os.Args[0]
|
||||
flag.Usage = func() {
|
||||
|
|
@ -82,11 +85,11 @@ func main() {
|
|||
cfg := setup.ParseFlags(true)
|
||||
|
||||
if *resetPassword {
|
||||
logrus.Fatalf("The reset-password flag has been replaced by the POST /_dendrite/admin/resetPassword/{localpart} admin API.")
|
||||
logger.Fatal().Msgf("The reset-password flag has been replaced by the POST /_dendrite/admin/resetPassword/{localpart} admin API.")
|
||||
}
|
||||
|
||||
if cfg.ClientAPI.RegistrationSharedSecret == "" {
|
||||
logrus.Fatalln("Shared secret registration is not enabled, enable it by setting a shared secret in the config: 'client_api.registration_shared_secret'")
|
||||
logger.Fatal().Msgf("Shared secret registration is not enabled, enable it by setting a shared secret in the config: 'client_api.registration_shared_secret'")
|
||||
}
|
||||
|
||||
if *username == "" {
|
||||
|
|
@ -95,17 +98,17 @@ func main() {
|
|||
}
|
||||
|
||||
if err := internal.ValidateUsername(*username, cfg.Global.ServerName); err != nil {
|
||||
logrus.WithError(err).Error("Specified username is invalid")
|
||||
logger.Error().Err(err).Msg("Specified username is invalid")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pass, err := getPassword(*password, *pwdFile, *pwdStdin, os.Stdin)
|
||||
if err != nil {
|
||||
logrus.Fatalln(err)
|
||||
logger.Fatal().Err(err)
|
||||
}
|
||||
|
||||
if err = internal.ValidatePassword(pass); err != nil {
|
||||
logrus.WithError(err).Error("Specified password is invalid")
|
||||
logger.Error().Err(err).Msg("Specified password is invalid")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
@ -113,10 +116,10 @@ func main() {
|
|||
|
||||
accessToken, err := sharedSecretRegister(cfg.ClientAPI.RegistrationSharedSecret, *serverURL, *username, pass, *isAdmin)
|
||||
if err != nil {
|
||||
logrus.Fatalln("Failed to create the account:", err.Error())
|
||||
logger.Fatal().Err(err).Msg("Failed to create the account.")
|
||||
}
|
||||
|
||||
logrus.Infof("Created account: %s (AccessToken: %s)", *username, accessToken)
|
||||
logger.Info().Msgf("Created account: %s (AccessToken: %s)", *username, accessToken)
|
||||
}
|
||||
|
||||
type sharedSecretRegistrationRequest struct {
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ func main() {
|
|||
cfg.MediaAPI.BasePath = config.Path(filepath.Join(*dirPath, "media"))
|
||||
cfg.Global.JetStream.StoragePath = config.Path(*dirPath)
|
||||
cfg.SyncAPI.Fulltext.IndexPath = config.Path(filepath.Join(*dirPath, "searchindex"))
|
||||
cfg.Logging = []config.LogrusHook{
|
||||
cfg.Logging = []config.LogHook{
|
||||
{
|
||||
Type: "file",
|
||||
Level: "info",
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
|
|
@ -56,7 +55,7 @@ func main() {
|
|||
|
||||
if *tlsCertFile != "" || *tlsKeyFile != "" {
|
||||
if *tlsCertFile == "" || *tlsKeyFile == "" {
|
||||
log.Fatal("Zero or both of --tls-key and --tls-cert must be supplied")
|
||||
panic("Zero or both of --tls-key and --tls-cert must be supplied")
|
||||
}
|
||||
if *authorityCertFile == "" && *authorityKeyFile == "" {
|
||||
if err := test.NewTLSKey(*tlsKeyFile, *tlsCertFile, *keySize); err != nil {
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ var difference = flag.Bool("difference", false, "whether to calculate the differ
|
|||
func main() {
|
||||
ctx := context.Background()
|
||||
cfg := setup.ParseFlags(true)
|
||||
cfg.Logging = append(cfg.Logging[:0], config.LogrusHook{
|
||||
cfg.Logging = append(cfg.Logging[:0], config.LogHook{
|
||||
Type: "std",
|
||||
Level: "error",
|
||||
})
|
||||
|
|
|
|||
|
|
@ -27,44 +27,45 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/matrix-org/util"
|
||||
|
||||
"github.com/matrix-org/dugong"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
log "github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
)
|
||||
|
||||
// logrus is using a global variable when we're using `logrus.AddHook`
|
||||
// this unfortunately results in us adding the same hook multiple times.
|
||||
// This map ensures we only ever add one level hook.
|
||||
var stdLevelLogAdded = make(map[logrus.Level]bool)
|
||||
var stdLevelLogAdded = make(map[zerolog.Level]bool)
|
||||
var levelLogAddedMu = &sync.Mutex{}
|
||||
|
||||
type utcFormatter struct {
|
||||
logrus.Formatter
|
||||
zerolog.Formatter
|
||||
}
|
||||
|
||||
func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
entry.Time = entry.Time.UTC()
|
||||
return f.Formatter.Format(entry)
|
||||
func (f utcFormatter) Format(entry *zerolog.Event) ([]byte, error) {
|
||||
return f.Format(entry)
|
||||
}
|
||||
|
||||
// Logrus hook which wraps another hook and filters log entries according to their level.
|
||||
// (Note that we cannot use solely logrus.SetLevel, because Dendrite supports multiple
|
||||
// levels of logging at the same time.)
|
||||
type logLevelHook struct {
|
||||
level logrus.Level
|
||||
logrus.Hook
|
||||
level zerolog.Level
|
||||
zerolog.Hook
|
||||
}
|
||||
|
||||
// Levels returns all the levels supported by this hook.
|
||||
func (h *logLevelHook) Levels() []logrus.Level {
|
||||
levels := make([]logrus.Level, 0)
|
||||
|
||||
for _, level := range logrus.AllLevels {
|
||||
func (h *logLevelHook) Levels() []zerolog.Level {
|
||||
levels := make([]zerolog.Level, 0)
|
||||
var level zerolog.Level = -1
|
||||
for level <= 5 {
|
||||
if level <= h.level {
|
||||
levels = append(levels, level)
|
||||
}
|
||||
level = level + 1
|
||||
}
|
||||
|
||||
return levels
|
||||
|
|
@ -103,9 +104,14 @@ func SetupPprof() {
|
|||
func SetupStdLogging() {
|
||||
levelLogAddedMu.Lock()
|
||||
defer levelLogAddedMu.Unlock()
|
||||
logrus.SetReportCaller(true)
|
||||
logrus.SetFormatter(&utcFormatter{
|
||||
&logrus.TextFormatter{
|
||||
//zerolog.SetReportCaller(true)
|
||||
//TimeFieldFormat => dont need
|
||||
//DisableColors => dont need
|
||||
//DisableTimestamp => dont need
|
||||
//Quo0teEmptyFields => dont need
|
||||
//callprettyfier => what?
|
||||
/*zerolog.Formatter(&utcFormatter{
|
||||
&zerolog.TextFormatter{
|
||||
TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
|
||||
FullTimestamp: true,
|
||||
DisableColors: false,
|
||||
|
|
@ -113,46 +119,39 @@ func SetupStdLogging() {
|
|||
QuoteEmptyFields: true,
|
||||
CallerPrettyfier: callerPrettyfier,
|
||||
},
|
||||
})
|
||||
})*/
|
||||
}
|
||||
|
||||
// File type hooks should be provided a path to a directory to store log files
|
||||
func checkFileHookParams(params map[string]interface{}) {
|
||||
path, ok := params["path"]
|
||||
if !ok {
|
||||
logrus.Fatalf("Expecting a parameter \"path\" for logging hook of type \"file\"")
|
||||
log.Fatal().Msg("Expecting a parameter \"path\" for logging hook of type \"file\"")
|
||||
}
|
||||
|
||||
if _, ok := path.(string); !ok {
|
||||
logrus.Fatalf("Parameter \"path\" for logging hook of type \"file\" should be a string")
|
||||
log.Fatal().Msg("Parameter \"path\" for logging hook of type \"file\" should be a string")
|
||||
}
|
||||
}
|
||||
|
||||
// Add a new FSHook to the logger. Each component will log in its own file
|
||||
func setupFileHook(hook config.LogrusHook, level logrus.Level) {
|
||||
func setupFileHook(hook config.LogHook, level zerolog.Level) {
|
||||
dirPath := (hook.Params["path"]).(string)
|
||||
fullPath := filepath.Join(dirPath, "dendrite.log")
|
||||
|
||||
if err := os.MkdirAll(path.Dir(fullPath), os.ModePerm); err != nil {
|
||||
logrus.Fatalf("Couldn't create directory %s: %q", path.Dir(fullPath), err)
|
||||
log.Fatal().Msgf("Couldn't create directory %s: %q", path.Dir(fullPath), err)
|
||||
}
|
||||
|
||||
logrus.AddHook(&logLevelHook{
|
||||
level,
|
||||
dugong.NewFSHook(
|
||||
fullPath,
|
||||
&utcFormatter{
|
||||
&logrus.TextFormatter{
|
||||
TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
|
||||
DisableColors: true,
|
||||
DisableTimestamp: false,
|
||||
DisableSorting: false,
|
||||
QuoteEmptyFields: true,
|
||||
},
|
||||
},
|
||||
&dugong.DailyRotationSchedule{GZip: true},
|
||||
),
|
||||
})
|
||||
/*
|
||||
log.Hook(&logLevelHook{
|
||||
level,
|
||||
dugong.NewFSHook(
|
||||
fullPath,
|
||||
&utcFormatter{log.Logger},
|
||||
&dugong.DailyRotationSchedule{GZip: true},
|
||||
),
|
||||
})*/
|
||||
/// TODO: Dugong FSHook!
|
||||
}
|
||||
|
||||
// CloseAndLogIfError Closes io.Closer and logs the error if any
|
||||
|
|
|
|||
|
|
@ -19,11 +19,10 @@ package internal
|
|||
|
||||
import (
|
||||
"io"
|
||||
"log/syslog"
|
||||
"os"
|
||||
|
||||
"github.com/MFAshby/stdemuxerhook"
|
||||
"github.com/sirupsen/logrus"
|
||||
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
||||
"github.com/rs/zerolog"
|
||||
log "github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
)
|
||||
|
|
@ -31,20 +30,20 @@ import (
|
|||
// SetupHookLogging configures the logging hooks defined in the configuration.
|
||||
// If something fails here it means that the logging was improperly configured,
|
||||
// so we just exit with the error
|
||||
func SetupHookLogging(hooks []config.LogrusHook) {
|
||||
func SetupHookLogging(hooks []config.LogHook) {
|
||||
levelLogAddedMu.Lock()
|
||||
defer levelLogAddedMu.Unlock()
|
||||
for _, hook := range hooks {
|
||||
// Check we received a proper logging level
|
||||
level, err := logrus.ParseLevel(hook.Level)
|
||||
level, err := zerolog.ParseLevel(hook.Level)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Unrecognised logging level %s: %q", hook.Level, err)
|
||||
log.Fatal().Msgf("Unrecognised logging level %s: %q", hook.Level, err)
|
||||
}
|
||||
|
||||
// Perform a first filter on the logs according to the lowest level of all
|
||||
// (Eg: If we have hook for info and above, prevent logrus from processing debug logs)
|
||||
if logrus.GetLevel() < level {
|
||||
logrus.SetLevel(level)
|
||||
if log.Logger.GetLevel() < level {
|
||||
log.Logger = log.Logger.Level(level)
|
||||
}
|
||||
|
||||
switch hook.Type {
|
||||
|
|
@ -57,46 +56,50 @@ func SetupHookLogging(hooks []config.LogrusHook) {
|
|||
case "std":
|
||||
setupStdLogHook(level)
|
||||
default:
|
||||
logrus.Fatalf("Unrecognised logging hook type: %s", hook.Type)
|
||||
log.Fatal().Msgf("Unrecognised logging hook type: %s", hook.Type)
|
||||
}
|
||||
}
|
||||
setupStdLogHook(logrus.InfoLevel)
|
||||
setupStdLogHook(zerolog.InfoLevel)
|
||||
// Hooks are now configured for stdout/err, so throw away the default logger output
|
||||
logrus.SetOutput(io.Discard)
|
||||
log.Logger = log.Logger.Output(io.Discard)
|
||||
}
|
||||
|
||||
func checkSyslogHookParams(params map[string]interface{}) {
|
||||
addr, ok := params["address"]
|
||||
if !ok {
|
||||
logrus.Fatalf("Expecting a parameter \"address\" for logging hook of type \"syslog\"")
|
||||
log.Fatal().Msg("Expecting a parameter \"address\" for logging hook of type \"syslog\"")
|
||||
}
|
||||
|
||||
if _, ok := addr.(string); !ok {
|
||||
logrus.Fatalf("Parameter \"address\" for logging hook of type \"syslog\" should be a string")
|
||||
log.Fatal().Msg("Parameter \"address\" for logging hook of type \"syslog\" should be a string")
|
||||
}
|
||||
|
||||
proto, ok2 := params["protocol"]
|
||||
if !ok2 {
|
||||
logrus.Fatalf("Expecting a parameter \"protocol\" for logging hook of type \"syslog\"")
|
||||
log.Fatal().Msg("Expecting a parameter \"protocol\" for logging hook of type \"syslog\"")
|
||||
}
|
||||
|
||||
if _, ok2 := proto.(string); !ok2 {
|
||||
logrus.Fatalf("Parameter \"protocol\" for logging hook of type \"syslog\" should be a string")
|
||||
log.Fatal().Msg("Parameter \"protocol\" for logging hook of type \"syslog\" should be a string")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func setupStdLogHook(level logrus.Level) {
|
||||
func setupStdLogHook(level zerolog.Level) {
|
||||
if stdLevelLogAdded[level] {
|
||||
return
|
||||
}
|
||||
logrus.AddHook(&logLevelHook{level, stdemuxerhook.New(logrus.StandardLogger())})
|
||||
//log.Logger.Hook(&logLevelHook{level, stdemuxerhook.New(log.Logger)})
|
||||
//log.Logger.Hook(&logLevelHook{level, zerolog.New(os.Stdout)})
|
||||
log.Logger = zerolog.New(os.Stdout)
|
||||
stdLevelLogAdded[level] = true
|
||||
}
|
||||
|
||||
func setupSyslogHook(hook config.LogrusHook, level logrus.Level) {
|
||||
syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, "dendrite")
|
||||
func setupSyslogHook(hook config.LogHook, level zerolog.Level) {
|
||||
// rewrite to:
|
||||
// https://stackoverflow.com/questions/73064915/logging-to-syslog-file-with-zerolog-golang
|
||||
/*syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, "dendrite")
|
||||
if err == nil {
|
||||
logrus.AddHook(&logLevelHook{level, syslogHook})
|
||||
}
|
||||
log.Logger.Hook(&logLevelHook{level, syslogHook})
|
||||
}*/
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,14 +27,18 @@ import (
|
|||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/gomatrixserverlib/spec"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"golang.org/x/crypto/ed25519"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/uber/jaeger-client-go"
|
||||
jaegerconfig "github.com/uber/jaeger-client-go/config"
|
||||
jaegermetrics "github.com/uber/jaeger-lib/metrics"
|
||||
)
|
||||
|
||||
var logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||
|
||||
// keyIDRegexp defines allowable characters in Key IDs.
|
||||
var keyIDRegexp = regexp.MustCompile("^ed25519:[a-zA-Z0-9_]+$")
|
||||
|
||||
|
|
@ -74,8 +78,8 @@ type Dendrite struct {
|
|||
Jaeger jaegerconfig.Configuration `yaml:"jaeger"`
|
||||
} `yaml:"tracing"`
|
||||
|
||||
// The config for logging informations. Each hook will be added to logrus.
|
||||
Logging []LogrusHook `yaml:"logging"`
|
||||
// The config for logging informations. Each hook will be added.
|
||||
Logging []LogHook `yaml:"logging"`
|
||||
|
||||
// Any information derived from the configuration options for later use.
|
||||
Derived Derived `yaml:"-"`
|
||||
|
|
@ -146,10 +150,10 @@ type ThumbnailSize struct {
|
|||
ResizeMethod string `yaml:"method,omitempty"`
|
||||
}
|
||||
|
||||
// LogrusHook represents a single logrus hook. At this point, only parsing and
|
||||
// LogHook represents a single log hook. At this point, only parsing and
|
||||
// verification of the proper values for type and level are done.
|
||||
// Validity/integrity checks on the parameters are done when configuring logrus.
|
||||
type LogrusHook struct {
|
||||
// Validity/integrity checks on the parameters are done when configuring log.
|
||||
type LogHook struct {
|
||||
// The type of hook, currently only "file" is supported.
|
||||
Type string `yaml:"type"`
|
||||
|
||||
|
|
@ -398,9 +402,9 @@ func checkPositive(configErrs *ConfigErrors, key string, value int64) {
|
|||
|
||||
// checkLogging verifies the parameters logging.* are valid.
|
||||
func (config *Dendrite) checkLogging(configErrs *ConfigErrors) {
|
||||
for _, logrusHook := range config.Logging {
|
||||
checkNotEmpty(configErrs, "logging.type", string(logrusHook.Type))
|
||||
checkNotEmpty(configErrs, "logging.level", string(logrusHook.Level))
|
||||
for _, logHook := range config.Logging {
|
||||
checkNotEmpty(configErrs, "logging.type", string(logHook.Type))
|
||||
checkNotEmpty(configErrs, "logging.level", string(logHook.Level))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -480,20 +484,31 @@ func (config *Dendrite) SetupTracing() (closer io.Closer, err error) {
|
|||
}
|
||||
return config.Tracing.Jaeger.InitGlobalTracer(
|
||||
"Dendrite",
|
||||
jaegerconfig.Logger(logrusLogger{logrus.StandardLogger()}),
|
||||
jaegerconfig.Logger(NewJaegerLogger(&logger)),
|
||||
jaegerconfig.Metrics(jaegermetrics.NullFactory),
|
||||
)
|
||||
}
|
||||
|
||||
// logrusLogger is a small wrapper that implements jaeger.Logger using logrus.
|
||||
type logrusLogger struct {
|
||||
l *logrus.Logger
|
||||
type jaegerLogger struct {
|
||||
logger *zerolog.Logger
|
||||
}
|
||||
|
||||
func (l logrusLogger) Error(msg string) {
|
||||
l.l.Error(msg)
|
||||
func NewJaegerLogger(logger *zerolog.Logger) jaeger.Logger {
|
||||
return &jaegerLogger{logger}
|
||||
}
|
||||
|
||||
func (l logrusLogger) Infof(msg string, args ...interface{}) {
|
||||
l.l.Infof(msg, args...)
|
||||
type Logger interface {
|
||||
// Error logs a message at error priority
|
||||
Error(msg string)
|
||||
|
||||
// Infof logs a message at info priority
|
||||
Infof(msg string, args ...interface{})
|
||||
}
|
||||
|
||||
func (jl *jaegerLogger) Error(msg string) {
|
||||
jl.logger.Error().Msg(msg)
|
||||
}
|
||||
|
||||
func (jl *jaegerLogger) Infof(msg string, args ...interface{}) {
|
||||
jl.logger.Info().Msgf(msg, args...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ import (
|
|||
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -30,6 +30,8 @@ var (
|
|||
enableRegistrationWithoutVerification = flag.Bool("really-enable-open-registration", false, "This allows open registration without secondary verification (reCAPTCHA). This is NOT RECOMMENDED and will SIGNIFICANTLY increase the risk that your server will be used to send spam or conduct attacks, which may result in your server being banned from rooms.")
|
||||
)
|
||||
|
||||
var logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||
|
||||
// ParseFlags parses the commandline flags and uses them to create a config.
|
||||
func ParseFlags(monolith bool) *config.Dendrite {
|
||||
flag.Parse()
|
||||
|
|
@ -40,13 +42,13 @@ func ParseFlags(monolith bool) *config.Dendrite {
|
|||
}
|
||||
|
||||
if *configPath == "" {
|
||||
logrus.Fatal("--config must be supplied")
|
||||
logger.Fatal().Msg("--config must be supplied")
|
||||
}
|
||||
|
||||
cfg, err := config.Load(*configPath)
|
||||
|
||||
if err != nil {
|
||||
logrus.Fatalf("Invalid config file: %s", err)
|
||||
logger.Fatal().Msgf("Invalid config file: %s", err)
|
||||
}
|
||||
|
||||
if *enableRegistrationWithoutVerification {
|
||||
|
|
|
|||
Loading…
Reference in a new issue