Add logging config and add some helper functions
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
config.yaml
|
||||
discord
|
||||
logs/
|
||||
registration.yaml
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
as "maunium.net/go/mautrix/appservice"
|
||||
)
|
||||
|
||||
type appservice struct {
|
||||
Address string `yaml:"address"`
|
||||
Hostname string `yaml:"hostname"`
|
||||
@@ -49,3 +53,16 @@ func (a *appservice) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
|
||||
return a.validate()
|
||||
}
|
||||
|
||||
func (cfg *Config) CreateAppService() (*as.AppService, error) {
|
||||
appservice := as.Create()
|
||||
|
||||
appservice.HomeserverURL = cfg.Homeserver.Address
|
||||
appservice.HomeserverDomain = cfg.Homeserver.Domain
|
||||
|
||||
appservice.Host.Hostname = cfg.Appservice.Hostname
|
||||
appservice.Host.Port = cfg.Appservice.Port
|
||||
appservice.DefaultHTTPRetries = 4
|
||||
|
||||
return appservice, nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ type Config struct {
|
||||
Homeserver homeserver `yaml:"homeserver"`
|
||||
Appservice appservice `yaml:"appservice"`
|
||||
Bridge bridge `yaml:"bridge"`
|
||||
Logging logging `yaml:"logging"`
|
||||
}
|
||||
|
||||
func (cfg *Config) validate() error {
|
||||
@@ -25,6 +26,10 @@ func (cfg *Config) validate() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Logging.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
89
config/logging.go
Normal file
89
config/logging.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"maunium.net/go/maulogger/v2"
|
||||
as "maunium.net/go/mautrix/appservice"
|
||||
)
|
||||
|
||||
type logging as.LogConfig
|
||||
|
||||
func (l *logging) validate() error {
|
||||
if l.Directory == "" {
|
||||
l.Directory = "./logs"
|
||||
}
|
||||
|
||||
if l.FileNameFormat == "" {
|
||||
l.FileNameFormat = "{{.Date}}-{{.Index}}.log"
|
||||
}
|
||||
|
||||
if l.FileDateFormat == "" {
|
||||
l.FileDateFormat = "2006-01-02"
|
||||
}
|
||||
|
||||
if l.FileMode == 0 {
|
||||
l.FileMode = 384
|
||||
}
|
||||
|
||||
if l.TimestampFormat == "" {
|
||||
l.TimestampFormat = "Jan _2, 2006 15:04:05"
|
||||
}
|
||||
|
||||
if l.RawPrintLevel == "" {
|
||||
l.RawPrintLevel = "debug"
|
||||
} else {
|
||||
switch strings.ToUpper(l.RawPrintLevel) {
|
||||
case "TRACE":
|
||||
l.PrintLevel = -10
|
||||
case "DEBUG":
|
||||
l.PrintLevel = maulogger.LevelDebug.Severity
|
||||
case "INFO":
|
||||
l.PrintLevel = maulogger.LevelInfo.Severity
|
||||
case "WARN", "WARNING":
|
||||
l.PrintLevel = maulogger.LevelWarn.Severity
|
||||
case "ERR", "ERROR":
|
||||
l.PrintLevel = maulogger.LevelError.Severity
|
||||
case "FATAL":
|
||||
l.PrintLevel = maulogger.LevelFatal.Severity
|
||||
default:
|
||||
return errors.New("invalid print level " + l.RawPrintLevel)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *logging) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
type rawLogging logging
|
||||
|
||||
raw := rawLogging{}
|
||||
if err := unmarshal(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*l = logging(raw)
|
||||
|
||||
return l.validate()
|
||||
}
|
||||
|
||||
func (cfg *Config) CreateLogger() (maulogger.Logger, error) {
|
||||
logger := maulogger.Create()
|
||||
|
||||
// create an as.LogConfig from our config so we can configure the logger
|
||||
realLogConfig := as.LogConfig(cfg.Logging)
|
||||
realLogConfig.Configure(logger)
|
||||
|
||||
// Set the default logger.
|
||||
maulogger.DefaultLogger = logger.(*maulogger.BasicLogger)
|
||||
|
||||
// If we were given a filename format attempt to open the file.
|
||||
if cfg.Logging.FileNameFormat != "" {
|
||||
if err := maulogger.OpenFile(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
@@ -31,3 +31,16 @@ func (cfg *Config) CopyToRegistration(registration *as.Registration) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *Config) getRegistration() (*as.Registration, error) {
|
||||
registration := as.CreateRegistration()
|
||||
|
||||
if err := cfg.CopyToRegistration(registration); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
registration.AppToken = cfg.Appservice.ASToken
|
||||
registration.ServerToken = cfg.Appservice.HSToken
|
||||
|
||||
return registration, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user