From e8e478f2aacecaa7b76556f7a6d66c244059aa7f Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 25 Jun 2024 10:43:43 +0800 Subject: [PATCH] feat: print request duration --- handlerutil/handlerutil.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/handlerutil/handlerutil.go b/handlerutil/handlerutil.go index 8fd27ae..a5959fb 100644 --- a/handlerutil/handlerutil.go +++ b/handlerutil/handlerutil.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "strings" + "time" ) type Middleware func(http.Handler) http.Handler @@ -29,9 +30,15 @@ func TrimPathSuffix(suffix string) Middleware { func Log(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + begin := time.Now() sw := &statusWriter{ResponseWriter: w} 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) }) }