internal: rework global logging settings, embedded outpost no longer overwrites core, clean up double init

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-12-20 21:23:19 +01:00
parent 3da526f20e
commit 621773c1ea
5 changed files with 54 additions and 36 deletions

View File

@ -26,6 +26,12 @@ Optionally, you can set these:
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyMsg: "event",
log.FieldKeyTime: "timestamp",
},
})
akURL, found := os.LookupEnv("AUTHENTIK_HOST") akURL, found := os.LookupEnv("AUTHENTIK_HOST")
if !found { if !found {
fmt.Println("env AUTHENTIK_HOST not set!") fmt.Println("env AUTHENTIK_HOST not set!")

View File

@ -21,7 +21,12 @@ var running = true
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.JSONFormatter{}) log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyMsg: "event",
log.FieldKeyTime: "timestamp",
},
})
l := log.WithField("logger", "authentik.root") l := log.WithField("logger", "authentik.root")
config.DefaultConfig() config.DefaultConfig()
err := config.LoadConfig("./authentik/lib/default.yml") err := config.LoadConfig("./authentik/lib/default.yml")
@ -73,9 +78,9 @@ func main() {
<-ex <-ex
running = false running = false
l.WithField("logger", "authentik").Info("shutting down gunicorn") l.Info("shutting down gunicorn")
go g.Kill() go g.Kill()
l.WithField("logger", "authentik").Info("shutting down webserver") l.Info("shutting down webserver")
go ws.Shutdown() go ws.Shutdown()
} }
} }
@ -93,8 +98,9 @@ func attemptStartBackend(g *gounicorn.GoUnicorn) {
func attemptProxyStart(ws *web.WebServer, u *url.URL) { func attemptProxyStart(ws *web.WebServer, u *url.URL) {
maxTries := 100 maxTries := 100
attempt := 0 attempt := 0
l := log.WithField("logger", "authentik.server")
for { for {
log.WithField("logger", "authentik").Debug("attempting to init outpost") l.Debug("attempting to init outpost")
ac := ak.NewAPIController(*u, config.G.SecretKey) ac := ak.NewAPIController(*u, config.G.SecretKey)
if ac == nil { if ac == nil {
attempt += 1 attempt += 1
@ -107,10 +113,10 @@ func attemptProxyStart(ws *web.WebServer, u *url.URL) {
srv := proxyv2.NewProxyServer(ac, 0) srv := proxyv2.NewProxyServer(ac, 0)
ws.ProxyServer = srv ws.ProxyServer = srv
ac.Server = srv ac.Server = srv
log.WithField("logger", "authentik").Debug("attempting to start outpost") l.Debug("attempting to start outpost")
err := ac.StartBackgorundTasks() err := ac.StartBackgorundTasks()
if err != nil { if err != nil {
log.WithField("logger", "authentik").WithError(err).Warning("outpost failed to start") l.WithError(err).Warning("outpost failed to start")
attempt += 1 attempt += 1
time.Sleep(15 * time.Second) time.Sleep(15 * time.Second)
if attempt > maxTries { if attempt > maxTries {

View File

@ -2,6 +2,7 @@ package config
import ( import (
"io/ioutil" "io/ioutil"
"strings"
env "github.com/Netflix/go-env" env "github.com/Netflix/go-env"
"github.com/imdario/mergo" "github.com/imdario/mergo"
@ -62,7 +63,7 @@ func FromEnv() error {
} }
func ConfigureLogger() { func ConfigureLogger() {
switch G.LogLevel { switch strings.ToLower(G.LogLevel) {
case "trace": case "trace":
log.SetLevel(log.TraceLevel) log.SetLevel(log.TraceLevel)
case "debug": case "debug":
@ -77,14 +78,14 @@ func ConfigureLogger() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
fm := log.FieldMap{
log.FieldKeyMsg: "event",
log.FieldKeyTime: "timestamp",
}
if G.Debug { if G.Debug {
log.SetFormatter(&log.TextFormatter{}) log.SetFormatter(&log.TextFormatter{FieldMap: fm})
} else { } else {
log.SetFormatter(&log.JSONFormatter{ log.SetFormatter(&log.JSONFormatter{FieldMap: fm})
FieldMap: log.FieldMap{
log.FieldKeyMsg: "event",
log.FieldKeyTime: "timestamp",
},
})
} }
} }

View File

@ -194,7 +194,13 @@ func (ac *APIController) startWSHealth() {
func (ac *APIController) startIntervalUpdater() { func (ac *APIController) startIntervalUpdater() {
logger := ac.logger.WithField("loop", "interval-updater") logger := ac.logger.WithField("loop", "interval-updater")
ticker := time.NewTicker(5 * time.Minute) ticker := time.NewTicker(5 * time.Minute)
initial := false
for ; true; <-ticker.C { for ; true; <-ticker.C {
if !initial {
initial = true
continue
}
logger.Debug("Running interval update")
err := ac.OnRefresh() err := ac.OnRefresh()
if err != nil { if err != nil {
logger.WithError(err).Debug("Failed to update") logger.WithError(err).Debug("Failed to update")

View File

@ -13,31 +13,30 @@ import (
) )
func doGlobalSetup(outpost api.Outpost, globalConfig api.Config) { func doGlobalSetup(outpost api.Outpost, globalConfig api.Config) {
log.SetFormatter(&log.JSONFormatter{ l := log.WithField("logger", "authentik.outpost")
FieldMap: log.FieldMap{ if !outpost.Managed.IsSet() {
log.FieldKeyMsg: "event", switch outpost.Config[ConfigLogLevel].(string) {
log.FieldKeyTime: "timestamp", case "trace":
}, log.SetLevel(log.TraceLevel)
}) case "debug":
switch outpost.Config[ConfigLogLevel].(string) { log.SetLevel(log.DebugLevel)
case "trace": case "info":
log.SetLevel(log.TraceLevel) log.SetLevel(log.InfoLevel)
case "debug": case "warning":
log.SetLevel(log.DebugLevel) log.SetLevel(log.WarnLevel)
case "info": case "error":
log.SetLevel(log.InfoLevel) log.SetLevel(log.ErrorLevel)
case "warning": default:
log.SetLevel(log.WarnLevel) log.SetLevel(log.DebugLevel)
case "error": }
log.SetLevel(log.ErrorLevel) } else {
default: l.Debug("Managed outpost, not seting global log level")
log.SetLevel(log.DebugLevel)
} }
log.WithField("logger", "authentik.outpost").WithField("hash", constants.BUILD()).WithField("version", constants.VERSION).Info("Starting authentik outpost") l.WithField("hash", constants.BUILD()).WithField("version", constants.VERSION).Info("Starting authentik outpost")
if globalConfig.ErrorReporting.Enabled { if globalConfig.ErrorReporting.Enabled {
dsn := "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8" dsn := "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8"
log.WithField("env", globalConfig.ErrorReporting.Environment).Debug("Error reporting enabled") l.WithField("env", globalConfig.ErrorReporting.Environment).Debug("Error reporting enabled")
err := sentry.Init(sentry.ClientOptions{ err := sentry.Init(sentry.ClientOptions{
Dsn: dsn, Dsn: dsn,
Environment: globalConfig.ErrorReporting.Environment, Environment: globalConfig.ErrorReporting.Environment,
@ -47,7 +46,7 @@ func doGlobalSetup(outpost api.Outpost, globalConfig api.Config) {
}, },
}) })
if err != nil { if err != nil {
log.WithField("env", globalConfig.ErrorReporting.Environment).WithError(err).Warning("Failed to initialise sentry") l.WithField("env", globalConfig.ErrorReporting.Environment).WithError(err).Warning("Failed to initialise sentry")
} }
} }
} }