38 lines
886 B
Go
38 lines
886 B
Go
|
package web
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-chi/chi/v5/middleware"
|
||
|
)
|
||
|
|
||
|
// LoggingMiddleware is used for logging info about requests to the servers configured accesslogger.
|
||
|
func (s *Server) LoggingMiddleware(next http.Handler) http.Handler {
|
||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||
|
t1 := time.Now()
|
||
|
|
||
|
reqID := middleware.GetReqID(r.Context())
|
||
|
|
||
|
defer func() {
|
||
|
/*s.AccessLogger.Debugw(r.Method,
|
||
|
"path", r.URL.Path,
|
||
|
"request-headers", r.Header,
|
||
|
"response-headers", ww.Header(),
|
||
|
)
|
||
|
*/
|
||
|
s.AccessLogger.Infow(r.Method,
|
||
|
"path", r.URL.Path,
|
||
|
"status", ww.Status(),
|
||
|
"written", ww.BytesWritten(),
|
||
|
"remote_addr", r.RemoteAddr,
|
||
|
"processing_time_ms", time.Since(t1).Milliseconds(),
|
||
|
"req_id", reqID)
|
||
|
}()
|
||
|
|
||
|
next.ServeHTTP(ww, r)
|
||
|
}
|
||
|
return http.HandlerFunc(fn)
|
||
|
}
|