From 506834c88157a2f9737b569f3e8f1693a099098f Mon Sep 17 00:00:00 2001 From: mStar Date: Sun, 4 May 2025 19:28:03 +0200 Subject: [PATCH 1/3] Extend logging middleware with more config options --- http/zerolog.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/http/zerolog.go b/http/zerolog.go index 61cec54..249baa3 100644 --- a/http/zerolog.go +++ b/http/zerolog.go @@ -5,19 +5,36 @@ import ( "strings" "time" + "github.com/rs/zerolog" "github.com/rs/zerolog/hlog" "github.com/rs/zerolog/log" ) -func BuildLoggingMiddleware(extras map[string]string) HandlerBuilder { +func BuildLoggingMiddleware( + status500IsError bool, + ignorePaths []string, + extras map[string]string, +) HandlerBuilder { return func(h http.Handler) http.Handler { return ChainMiddlewares(h, hlog.NewHandler(log.Logger), hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) { - if strings.HasPrefix(r.URL.Path, "/assets") { - return + for _, p := range ignorePaths { + if strings.HasPrefix(r.URL.Path, p) { + return + } } - logger := hlog.FromRequest(r).Info(). + var logger *zerolog.Event + if status >= 500 { + if status500IsError { + logger = hlog.FromRequest(r).Error() + } else { + logger = hlog.FromRequest(r).Warn() + } + } else { + logger = hlog.FromRequest(r).Info() + } + logger = logger. Str("method", r.Method). Stringer("url", r.URL). Int("status", status). From d7ed5b1eae6aa3eebae1898221798db14cf6d192 Mon Sep 17 00:00:00 2001 From: mStar Date: Mon, 12 May 2025 15:08:53 +0200 Subject: [PATCH 2/3] fix(webutils): Bad Ip logging Fix the logging middleware only logging the last proxy's ip instead of the ip of the actual request --- http/zerolog.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/http/zerolog.go b/http/zerolog.go index 249baa3..026ea9b 100644 --- a/http/zerolog.go +++ b/http/zerolog.go @@ -45,7 +45,7 @@ func BuildLoggingMiddleware( } logger.Send() }), - hlog.RemoteAddrHandler("ip"), + RealIpAppenderMiddleware("ip"), hlog.UserAgentHandler("user_agent"), hlog.RefererHandler("referer"), hlog.RequestIDHandler("req_id", "Request-Id"), @@ -68,9 +68,33 @@ func LoggingMiddleware(handler http.Handler) http.Handler { Dur("duration", duration). Send() }), - hlog.RemoteAddrHandler("ip"), + RealIpAppenderMiddleware("ip"), hlog.UserAgentHandler("user_agent"), hlog.RefererHandler("referer"), hlog.RequestIDHandler("req_id", "Request-Id"), ) } + +// hlog.RemoteAddrHandler except fixed to check the X-Real-Ip and X-Forwarded-For +// headers first for the IP instead of relying on RemoteAddr +// (which would only return the last proxy's address instead of the caller's) +func RealIpAppenderMiddleware(fieldKey string) func(handler http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + IPAddress := r.Header.Get("X-Real-Ip") + if IPAddress == "" { + IPAddress = r.Header.Get("X-Forwarded-For") + } + if IPAddress == "" { + IPAddress = r.RemoteAddr + } + if IPAddress != "" { + log := zerolog.Ctx(r.Context()) + log.UpdateContext(func(c zerolog.Context) zerolog.Context { + return c.Str(fieldKey, r.RemoteAddr) + }) + } + next.ServeHTTP(w, r) + }) + } +} From 4eb5d68fbfa7a51f5983da226f85137c41b81b9c Mon Sep 17 00:00:00 2001 From: mStar Date: Mon, 12 May 2025 15:35:45 +0200 Subject: [PATCH 3/3] Fix for the fix I didn't actually write the found ip to the logging stack (or whatever zerolog uses). This is now fixed --- http/zerolog.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http/zerolog.go b/http/zerolog.go index 026ea9b..0249d21 100644 --- a/http/zerolog.go +++ b/http/zerolog.go @@ -91,7 +91,7 @@ func RealIpAppenderMiddleware(fieldKey string) func(handler http.Handler) http.H if IPAddress != "" { log := zerolog.Ctx(r.Context()) log.UpdateContext(func(c zerolog.Context) zerolog.Context { - return c.Str(fieldKey, r.RemoteAddr) + return c.Str(fieldKey, IPAddress) }) } next.ServeHTTP(w, r)