package main import ( "fmt" "log" "net/http" "time" ) type LoggingHTTPHandler struct { Handler http.Handler } func (l *LoggingHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { t1 := time.Now().UTC() l.Handler.ServeHTTP(w, r) log.Println( fmt.Sprintf("%s %s %s %v", getIP(r), r.Method, r.URL.Path, time.Since(t1)), ) } 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 }