32 lines
727 B
Go
32 lines
727 B
Go
package httpserver
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func loggerMiddleware(l *zap.Logger) func(next http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
|
|
t1 := time.Now()
|
|
defer func() {
|
|
l.Info("HTTP",
|
|
zap.String("proto", r.Proto),
|
|
zap.String("path", r.URL.Path),
|
|
zap.Duration("dur", time.Since(t1)),
|
|
zap.Int("status", ww.Status()),
|
|
zap.Int("size", ww.BytesWritten()),
|
|
zap.String("reqId", middleware.GetReqID(r.Context())))
|
|
}()
|
|
|
|
next.ServeHTTP(ww, r)
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
}
|