From e74221612e8bf901b56cc3742798c0b5d6c2ce4e Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Thu, 29 Jan 2026 16:01:33 -0800 Subject: [PATCH 1/2] Log response size in http middleware logger --- lmw.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lmw.go b/lmw.go index f93a203..ded18ff 100644 --- a/lmw.go +++ b/lmw.go @@ -79,7 +79,8 @@ func LoggingMiddleware(longQueryDuration int, getUserName func(context.Context) Str("method", r.Method). Str("path", r.URL.EscapedPath()). Str("query", r.URL.Query().Encode()). - Int("status", wr.status) + Int("status", wr.status). + Int("response_size", wr.size) // Add duration info if durationMs > int64(longQueryDuration) { @@ -97,9 +98,10 @@ func LoggingMiddleware(longQueryDuration int, getUserName func(context.Context) // https://blog.questionable.services/article/guide-logging-middleware-go/ // responseWriter is a minimal wrapper for http.ResponseWriter that allows the -// written HTTP status code to be captured for logging. +// written HTTP status code and response size to be captured for logging. type responseWriter struct { status int + size int wroteHeader bool http.ResponseWriter } @@ -125,5 +127,7 @@ func (rw *responseWriter) Write(response []byte) (int, error) { rw.status = http.StatusOK rw.wroteHeader = true } - return rw.ResponseWriter.Write(response) + n, err := rw.ResponseWriter.Write(response) + rw.size += n + return n, err } From 6acbed285f8b2c980087647dc61146d8b46d694d Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Thu, 29 Jan 2026 16:08:38 -0800 Subject: [PATCH 2/2] Int64 --- lmw.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lmw.go b/lmw.go index ded18ff..2a8eaba 100644 --- a/lmw.go +++ b/lmw.go @@ -80,7 +80,7 @@ func LoggingMiddleware(longQueryDuration int, getUserName func(context.Context) Str("path", r.URL.EscapedPath()). Str("query", r.URL.Query().Encode()). Int("status", wr.status). - Int("response_size", wr.size) + Int64("response_size", wr.size) // Add duration info if durationMs > int64(longQueryDuration) { @@ -101,7 +101,7 @@ func LoggingMiddleware(longQueryDuration int, getUserName func(context.Context) // written HTTP status code and response size to be captured for logging. type responseWriter struct { status int - size int + size int64 wroteHeader bool http.ResponseWriter } @@ -128,6 +128,6 @@ func (rw *responseWriter) Write(response []byte) (int, error) { rw.wroteHeader = true } n, err := rw.ResponseWriter.Write(response) - rw.size += n + rw.size += int64(n) return n, err }