From f7905bcf37a528f54dc27e1edd839d9823cad7cc Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Tue, 7 May 2019 23:09:16 +0200 Subject: [PATCH] Fix & improve remote address handling (on req. log) --- internal/middleware/logger.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/middleware/logger.go b/internal/middleware/logger.go index 76052c296..d6bbfd681 100644 --- a/internal/middleware/logger.go +++ b/internal/middleware/logger.go @@ -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) })