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()