weapp/pkg/web/middleware/request.go

24 lines
504 B
Go

package middleware
import (
"net/http"
"time"
"github.com/sirupsen/logrus"
)
func LogRequestMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
at := time.Now()
defer func() {
logrus.WithFields(logrus.Fields{
"RemoteAddr": r.RemoteAddr,
"Method": r.Method,
"URI": r.RequestURI,
"Cost": time.Since(at).String(),
}).Infof("[%s] %s", r.Method, r.RequestURI)
}()
next.ServeHTTP(w, r)
})
}