diff --git a/middleware/logging.go b/middleware/logging.go index c73b656..18a9737 100644 --- a/middleware/logging.go +++ b/middleware/logging.go @@ -3,13 +3,29 @@ package middleware import ( "log" "net/http" + "time" ) -func LoggingMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - log.Printf("[%s] %s", r.Method, r.URL.EscapedPath()) - next.ServeHTTP(w, r) - }, - ) +type wrappedWriter struct { + http.ResponseWriter + statusCode int +} + +func (w * wrappedWriter) WriteHeader(statusCode int) { + w.ResponseWriter.WriteHeader(statusCode) + w.statusCode = statusCode +} + +func LoggingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + wrapped := &wrappedWriter{ + ResponseWriter: w, + statusCode: http.StatusOK, + } + + next.ServeHTTP(wrapped, r) + log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start)) + }) }