From c0a73494c374339e45e43433e3f103964ccfbb96 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sun, 22 May 2022 12:06:23 +0200 Subject: [PATCH] Add request logging --- cmd/boardvoting/middleware.go | 8 ++++++++ cmd/boardvoting/routes.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/boardvoting/middleware.go b/cmd/boardvoting/middleware.go index f4d90ca..282f3f5 100644 --- a/cmd/boardvoting/middleware.go +++ b/cmd/boardvoting/middleware.go @@ -31,3 +31,11 @@ func secureHeaders(next http.Handler) http.Handler { next.ServeHTTP(w, r) }) } + +func (app *application) logRequest(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + app.infoLog.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL.RequestURI()) + + next.ServeHTTP(w, r) + }) +} diff --git a/cmd/boardvoting/routes.go b/cmd/boardvoting/routes.go index 9ba869b..42155dc 100644 --- a/cmd/boardvoting/routes.go +++ b/cmd/boardvoting/routes.go @@ -44,5 +44,5 @@ func (app *application) routes() http.Handler { mux.HandleFunc("/", app.home) mux.HandleFunc("/motions/", app.motionList) - return secureHeaders(mux) + return app.logRequest(secureHeaders(mux)) }