From 5e5f56e8be30016d8af52f89874ad895204ba990 Mon Sep 17 00:00:00 2001 From: Anton Bronnikov Date: Mon, 30 Oct 2023 14:32:34 +0200 Subject: [PATCH] [fix] Better report HTTP statuses --- httplogger/middleware.go | 4 ++-- httplogger/response_writer.go | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/httplogger/middleware.go b/httplogger/middleware.go index 95abf9e..569fa1a 100644 --- a/httplogger/middleware.go +++ b/httplogger/middleware.go @@ -46,9 +46,9 @@ func Middleware(logger *zap.Logger, next http.Handler) http.Handler { // Passing request stats both in-message (for the human reader) // as well as inside the structured log (for the machine parser) - logger.Info(fmt.Sprintf("%s: %s %s %d", r.URL.Scheme, r.Method, r.URL.EscapedPath(), wrapped.status), + logger.Info(fmt.Sprintf("%s: %s %s %d", r.URL.Scheme, r.Method, r.URL.EscapedPath(), wrapped.Status()), zap.Int("durationMs", int(time.Since(start).Milliseconds())), - zap.Int("status", wrapped.status), + zap.Int("status", wrapped.Status()), zap.String("httpRequestID", httpRequestID), zap.String("logType", "access"), zap.String("method", r.Method), diff --git a/httplogger/response_writer.go b/httplogger/response_writer.go index ce55ab7..0617e0d 100644 --- a/httplogger/response_writer.go +++ b/httplogger/response_writer.go @@ -15,7 +15,10 @@ func wrapResponseWriter(w http.ResponseWriter) *responseWriter { } func (rw *responseWriter) Status() int { - return rw.status + if rw.wroteHeader { + return rw.status + } + return http.StatusOK } func (rw *responseWriter) WriteHeader(code int) {