From 1fd65a5827330d536b52ef53e52faf4d8dca26df Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Tue, 12 Sep 2023 00:52:54 +0300 Subject: [PATCH] feat: Logger: log X-Real-IP when behind rProxy --- main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 944ca62..1d9dcfd 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,12 @@ func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) { tNow := time.Now().UTC() l.Handler.ServeHTTP(w, r) methodString := l.getMethodLogString(r.Method) - log.Printf(" %s %s %s %v", r.RemoteAddr, methodString, r.URL, time.Since(tNow)) + remote := r.RemoteAddr + realIP := r.Header.Get("X-Real-IP") + if realIP != "" { + remote = realIP + } + log.Printf(" %s %s %s %v", remote, methodString, r.URL, time.Since(tNow)) } func (l *Logger) getMethodLogString(method string) string {