Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Int64("response_size", wr.size)

// Add duration info
if durationMs > int64(longQueryDuration) {
Expand All @@ -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 int64
wroteHeader bool
http.ResponseWriter
}
Expand All @@ -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 += int64(n)
return n, err
}
Loading