Skip to content

Commit

Permalink
Added request logging middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojackson committed Jun 10, 2020
1 parent 180564d commit 459b8ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,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)
})
}
2 changes: 1 addition & 1 deletion cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ func (app *application) routes() http.Handler {
fileServer := http.FileServer(http.Dir("./ui/static/"))
mux.Handle("/static/", http.StripPrefix("/static", fileServer))

return secureHeaders(mux)
return app.logRequest(secureHeaders(mux))
}

0 comments on commit 459b8ea

Please sign in to comment.