27 lines
604 B
Go
27 lines
604 B
Go
package httpserver
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/felixge/httpsnoop"
|
|
)
|
|
|
|
func (h *httpServer) requestLoggingMiddleware(fn http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
metrics := httpsnoop.CaptureMetrics(fn, w, req)
|
|
|
|
h.logger.Info(
|
|
"HTTP request.",
|
|
"remote_addr", req.RemoteAddr,
|
|
"user_agent", req.UserAgent(),
|
|
"host", req.Host,
|
|
"path", fmt.Sprintf("%s %s", req.Method, req.RequestURI),
|
|
"duration", metrics.Duration,
|
|
"req_size", req.ContentLength,
|
|
"resp_size", metrics.Written,
|
|
"http_code", metrics.Code,
|
|
)
|
|
}
|
|
}
|