From 75476217a036f90213032b0a2d910d9f804f26e7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 4 Sep 2021 13:52:47 +0200 Subject: [PATCH] internal: fix web requests not having a logger set Signed-off-by: Jens Langhammer --- internal/web/middleware_log.go | 32 +++++++++++++++++--------------- internal/web/web.go | 5 +++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/internal/web/middleware_log.go b/internal/web/middleware_log.go index 026514e9b..246bbf452 100644 --- a/internal/web/middleware_log.go +++ b/internal/web/middleware_log.go @@ -9,19 +9,21 @@ import ( "goauthentik.io/internal/utils/web" ) -func loggingMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - span := sentry.StartSpan(r.Context(), "authentik.go.request") - before := time.Now() - // Call the next handler, which can be another middleware in the chain, or the final handler. - next.ServeHTTP(w, r) - after := time.Now() - log.WithFields(log.Fields{ - "remote": r.RemoteAddr, - "method": r.Method, - "took": after.Sub(before), - "host": web.GetHost(r), - }).Info(r.RequestURI) - span.Finish() - }) +func loggingMiddleware(l *log.Entry) func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + span := sentry.StartSpan(r.Context(), "authentik.go.request") + before := time.Now() + // Call the next handler, which can be another middleware in the chain, or the final handler. + next.ServeHTTP(w, r) + after := time.Now() + l.WithFields(log.Fields{ + "remote": r.RemoteAddr, + "method": r.Method, + "took": after.Sub(before), + "host": web.GetHost(r), + }).Info(r.RequestURI) + span.Finish() + }) + } } diff --git a/internal/web/web.go b/internal/web/web.go index 1c48b3871..4a62b7d0d 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -30,6 +30,7 @@ type WebServer struct { } func NewWebServer() *WebServer { + l := log.WithField("logger", "authentik.g.web") mainHandler := mux.NewRouter() if config.G.ErrorReporting.Enabled { mainHandler.Use(recoveryMiddleware()) @@ -37,14 +38,14 @@ func NewWebServer() *WebServer { mainHandler.Use(handlers.ProxyHeaders) mainHandler.Use(handlers.CompressHandler) logginRouter := mainHandler.NewRoute().Subrouter() - logginRouter.Use(loggingMiddleware) + logginRouter.Use(loggingMiddleware(l)) ws := &WebServer{ LegacyProxy: true, m: mainHandler, lh: logginRouter, - log: log.WithField("logger", "authentik.g.web"), + log: l, } ws.configureStatic() ws.configureProxy()