feat: print request duration
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Middleware func(http.Handler) http.Handler
|
type Middleware func(http.Handler) http.Handler
|
||||||
@@ -29,9 +30,15 @@ func TrimPathSuffix(suffix string) Middleware {
|
|||||||
|
|
||||||
func Log(next http.Handler) http.Handler {
|
func Log(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
begin := time.Now()
|
||||||
sw := &statusWriter{ResponseWriter: w}
|
sw := &statusWriter{ResponseWriter: w}
|
||||||
next.ServeHTTP(sw, r)
|
next.ServeHTTP(sw, r)
|
||||||
log.Printf("response %s %s %v", statusToBlock(sw.status), r.Method, r.URL)
|
elasped := time.Since(begin)
|
||||||
|
log.Printf("response %s %s %s %v",
|
||||||
|
statusToBlock(sw.status),
|
||||||
|
elasped.String(),
|
||||||
|
r.Method,
|
||||||
|
r.URL)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user