3
0

Fix & improve remote address handling (on req. log)

This commit is contained in:
Denis Arh
2019-05-07 23:09:16 +02:00
parent 7f04c2cc2a
commit f7905bcf37
+6 -1
View File
@@ -37,12 +37,17 @@ func ContextLogger(log *zap.Logger) func(next http.Handler) http.Handler {
// It uses logger from context, see ContextLogger()
func LogRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var remote = req.RemoteAddr
if l := strings.LastIndex(remote, ":"); l > -1 {
remote = remote[:l]
}
logger.ContextValue(req.Context()).Info(
"HTTP request "+req.Method+" "+req.URL.Path,
zap.String("method", req.Method),
zap.String("path", req.URL.Path),
zap.Int64("size", req.ContentLength),
zap.String("remote", req.RemoteAddr[:strings.Index(req.RemoteAddr, ":")]),
zap.String("remote", remote),
)
next.ServeHTTP(w, req)
})