2023-09-19 22:47:19 +02:00
|
|
|
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(
|
2023-10-02 20:50:07 +02:00
|
|
|
fmt.Sprintf("%s %s %s %v", getIP(r), r.Method, r.URL.Path, time.Since(t1)),
|
2023-09-19 22:47:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(handler http.Handler) LoggingHTTPHandler {
|
|
|
|
return LoggingHTTPHandler{handler}
|
|
|
|
}
|
2023-10-02 20:50:07 +02:00
|
|
|
|
|
|
|
func getIP(r *http.Request) string {
|
|
|
|
|
|
|
|
RealIP := r.Header.Get("X-Real-IP")
|
|
|
|
if RealIP == "" {
|
|
|
|
return r.RemoteAddr
|
|
|
|
}
|
|
|
|
return RealIP
|
|
|
|
}
|