Skip to content

Instantly share code, notes, and snippets.

@ivancevich
Last active July 26, 2016 15:03
Show Gist options
  • Save ivancevich/283be16f65f037b595eeecc6c9c7672e to your computer and use it in GitHub Desktop.
Save ivancevich/283be16f65f037b595eeecc6c9c7672e to your computer and use it in GitHub Desktop.
Golang Logging Middleware
func Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dump, _ := httputil.DumpRequest(r, true)
log.Println("-------------------- Request -------------------->")
log.Println(string(dump))
log.Println("<------------------- Request ---------------------")
c := httptest.NewRecorder()
next.ServeHTTP(c, r)
for k, v := range c.HeaderMap {
w.Header().Set(k, strings.Join(v, ""))
}
w.WriteHeader(c.Code)
log.Println("-------------------- Response -------------------->")
log.Println(string(c.Body.Bytes()))
log.Println("<------------------- Response ---------------------")
c.Body.WriteTo(w)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment