fix: Log real ip if X-RealIP is set

This commit is contained in:
HeshamTB 2023-10-02 21:50:07 +03:00
parent ec94bce2aa
commit 631adc6b56
1 changed files with 10 additions and 1 deletions

View File

@ -18,7 +18,7 @@ func (l *LoggingHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
l.Handler.ServeHTTP(w, r)
log.Println(
fmt.Sprintf("%s %s %s %v", r.RemoteAddr, r.Method, r.URL.Path, time.Since(t1)),
fmt.Sprintf("%s %s %s %v", getIP(r), r.Method, r.URL.Path, time.Since(t1)),
)
}
@ -26,3 +26,12 @@ func (l *LoggingHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func NewLogger(handler http.Handler) LoggingHTTPHandler {
return LoggingHTTPHandler{handler}
}
func getIP(r *http.Request) string {
RealIP := r.Header.Get("X-Real-IP")
if RealIP == "" {
return r.RemoteAddr
}
return RealIP
}