Log HTTP request
// logRequest logs the request processed by next. Each request is
// timed and it's response code logged.
func logRequest(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// initialize the status to 200 in case WriteHeader is not called
rec := statusRecorder{w, 200}
next.ServeHTTP(&rec, r)
debug.Println(r.Method, r.URL.Path, rec.status, time.Since(start))
}
}
type statusRecorder struct {
http.ResponseWriter
status int
}
// record status code
func (rec *statusRecorder) WriteHeader(code int) {
rec.status = code
rec.ResponseWriter.WriteHeader(code)
}