extract and de-duplicate HTTP status capture
1 file changed, 4 insertions(+), 19 deletions(-)
changed files
M domain/web/server/logging.go → domain/web/server/logging.go
@@ -3,34 +3,19 @@ import ( "net/http" + sharedhttp "alin.ovh/homestead/shared/http" + "alin.ovh/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) + lw := sharedhttp.NewStatusCapturingResponseWriter(w) wrappedHandler.ServeHTTP(lw, r) log.Info( "http request", "method", r.Method, - "status", lw.statusCode, + "status", lw.Status, "host", r.Host, "path", r.URL.Path, "location", lw.Header().Get("Location"),