fix: Log real ip if X-RealIP is set
This commit is contained in:
parent
ec94bce2aa
commit
631adc6b56
11
logging.go
11
logging.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user