36 lines
792 B
Go
36 lines
792 B
Go
package middleware
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"log"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type statusRecorder struct {
|
||
|
|
http.ResponseWriter
|
||
|
|
statusCode int
|
||
|
|
}
|
||
|
|
|
||
|
|
func (rec *statusRecorder) WriteHeader(code int) {
|
||
|
|
rec.statusCode = code
|
||
|
|
rec.ResponseWriter.WriteHeader(code)
|
||
|
|
}
|
||
|
|
|
||
|
|
func Logger(next http.Handler) http.Handler {
|
||
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if r.URL.Path == "/health" {
|
||
|
|
start := time.Now()
|
||
|
|
next.ServeHTTP(w, r)
|
||
|
|
log.Printf("%s %s %s %v", r.Method, r.RequestURI, r.RemoteAddr, time.Since(start))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
start := time.Now()
|
||
|
|
rec := &statusRecorder{ResponseWriter: w, statusCode: http.StatusOK}
|
||
|
|
next.ServeHTTP(rec, r)
|
||
|
|
duration := time.Since(start)
|
||
|
|
|
||
|
|
log.Printf("%s %s %s %d %v", r.Method, r.RequestURI, r.RemoteAddr, rec.statusCode, duration)
|
||
|
|
})
|
||
|
|
}
|