gosimplenpm/middlewares/logRequest.go

30 lines
602 B
Go

package middlewares
import (
"net/http"
"net/http/httputil"
"github.com/sirupsen/logrus"
)
func LogMiddleware(lg *logrus.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lg.Debugf("%s - %s - %s", r.Method, r.URL, r.Host)
hasBody := false
if r.Method == "PUT" {
hasBody = true
}
requestDump, err := httputil.DumpRequest(r, hasBody)
if err != nil {
lg.Debugln(err)
}
lg.Debugln("RequestDump: ", string(requestDump))
next.ServeHTTP(w, r)
})
}
}