Skip to content

Commit

Permalink
add in writeheader
Browse files Browse the repository at this point in the history
Signed-off-by: mcoops <cooper.d.mark@gmail.com>
  • Loading branch information
mcoops authored and ldelossa committed Sep 17, 2020
1 parent fb83eb6 commit e45c1e1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions httptransport/logginghandler.go
Expand Up @@ -2,7 +2,6 @@ package httptransport

import (
"net/http"
"strconv"
"time"

"github.com/rs/zerolog"
Expand All @@ -13,6 +12,11 @@ type httpStatusWriter struct {
StatusCode int
}

func (lrw *httpStatusWriter) WriteHeader(code int) {
lrw.StatusCode = code
lrw.ResponseWriter.WriteHeader(code)
}

// LoggingHandler will log HTTP requests using the pre initialized zerolog.
func LoggingHandler(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -23,13 +27,13 @@ func LoggingHandler(next http.Handler) http.HandlerFunc {
// default HTTP StatusOK
lrw := &httpStatusWriter{ResponseWriter: w, StatusCode: http.StatusOK}

next.ServeHTTP(w, r)
next.ServeHTTP(lrw, r)

log.Info().
Str("remote addr", r.RemoteAddr).
Str("method", r.Method).
Str("request uri", r.RequestURI).
Str("status", strconv.Itoa(lrw.StatusCode)).
Int("status", lrw.StatusCode).
Float64("elapsed time (md)", float64(time.Since(start).Nanoseconds())*1e-6).
Msg("handled HTTP request")
}
Expand Down

0 comments on commit e45c1e1

Please sign in to comment.