-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
40 lines (32 loc) · 833 Bytes
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
)
type LoggedResponseWriter struct {
ResponseWriter http.ResponseWriter
StatusCode int
}
func (w *LoggedResponseWriter) Header() http.Header {
return w.ResponseWriter.Header()
}
func (w *LoggedResponseWriter) Write(body []byte) (int, error) {
return w.ResponseWriter.Write(body)
}
func (w *LoggedResponseWriter) WriteHeader(statusCode int) {
w.StatusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func logged(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
lrw := &LoggedResponseWriter{
ResponseWriter: w,
}
t0 := time.Now()
h(lrw, r, ps)
t := time.Since(t0)
log.Printf("%s %s - %d %v", r.Method, r.URL, lrw.StatusCode, t)
}
}