mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
33 lines
679 B
Go
33 lines
679 B
Go
package httpserver
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/logging"
|
|
)
|
|
|
|
func LoggerMiddleware(l logging.Logger) func(h http.Handler) http.Handler {
|
|
return func(h http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
r = r.WithContext(logging.ContextWithLogger(r.Context(), l))
|
|
|
|
// copy
|
|
method := r.Method
|
|
path := r.URL.Path
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
latency := time.Since(start)
|
|
|
|
l.WithFields(map[string]interface{}{
|
|
"method": method,
|
|
"path": path,
|
|
"latency": latency,
|
|
"user_agent": r.UserAgent(),
|
|
}).Info("Request")
|
|
})
|
|
}
|
|
}
|