Last active
July 26, 2016 15:03
-
-
Save ivancevich/283be16f65f037b595eeecc6c9c7672e to your computer and use it in GitHub Desktop.
Golang Logging Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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