package server import ( "net/http" "go.alanpearce.eu/x/log" ) type LoggingResponseWriter struct { http.ResponseWriter statusCode int } func (lrw *LoggingResponseWriter) WriteHeader(code int) { lrw.statusCode = code // avoids warning: superfluous response.WriteHeader call if lrw.statusCode != http.StatusOK { lrw.ResponseWriter.WriteHeader(code) } } func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter { return &LoggingResponseWriter{w, http.StatusOK} } func wrapHandlerWithLogging(wrappedHandler http.Handler, log *log.Logger) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { lw := NewLoggingResponseWriter(w) wrappedHandler.ServeHTTP(lw, r) if r.URL.Path == "/health" { return } log.Info( "http request", "method", r.Method, "status", lw.statusCode, "host", r.Host, "path", r.URL.Path, "location", lw.Header().Get("Location"), ) }) }