/
middleware_logger.go
46 lines (37 loc) · 1.22 KB
/
middleware_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package server
import (
"math/big"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
"github.com/digitalbitsorg/go/support/log"
)
func loggerMiddleware(requestIDKey interface{}, next http.Handler, w http.ResponseWriter, r *http.Request) {
requestLog := log.WithFields(log.F{
"request_id": r.Context().Value(requestIDKey),
"method": r.Method,
"uri": r.RequestURI,
"ip": r.RemoteAddr,
})
requestLog.Info("HTTP request")
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
requestStartTime := time.Now()
next.ServeHTTP(ww, r)
duration := big.NewRat(
time.Since(requestStartTime).Nanoseconds(),
int64(time.Second),
)
requestLog.WithFields(log.F{
"status": ww.Status(),
"response_bytes": ww.BytesWritten(),
"duration": duration.FloatString(8),
}).Info("HTTP response")
}
// BindLoggerMiddleware returns a LoggerMiddleware bound to the passed in requestIdKey, thereby decoupling it from middleware.RequestID
func BindLoggerMiddleware(requestIDKey interface{}) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
loggerMiddleware(requestIDKey, next, w, r)
})
}
}