31 lines
653 B
Go
31 lines
653 B
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
)
|
|
|
|
func (s *Server) MiddlewareLogging(next http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
|
|
reqID := middleware.GetReqID(r.Context())
|
|
|
|
t1 := time.Now()
|
|
defer func(ww middleware.WrapResponseWriter) {
|
|
s.Logger.Info("Served request.",
|
|
"status", ww.Status(),
|
|
"path", r.URL.Path,
|
|
"duration", time.Since(t1),
|
|
"remote", r.RemoteAddr,
|
|
"written", ww.BytesWritten(),
|
|
"req", reqID,
|
|
)
|
|
}(ww)
|
|
next.ServeHTTP(ww, r)
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|