-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttplogger.go
59 lines (51 loc) · 1.19 KB
/
httplogger.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"log"
"net/http"
"time"
)
type stResponseWriter struct {
http.ResponseWriter
HTTPStatus int
ResponseSize int
}
func (w *stResponseWriter) WriteHeader(status int) {
w.HTTPStatus = status
w.ResponseWriter.WriteHeader(status)
}
func (w *stResponseWriter) Flush() {
z := w.ResponseWriter
if f, ok := z.(http.Flusher); ok {
f.Flush()
}
}
func (w *stResponseWriter) CloseNotify() <-chan bool {
z := w.ResponseWriter
return z.(http.CloseNotifier).CloseNotify()
}
func (w *stResponseWriter) Write(b []byte) (int, error) {
if w.HTTPStatus == 0 {
w.HTTPStatus = 200
}
w.ResponseSize = len(b)
return w.ResponseWriter.Write(b)
}
func HTTPLogger(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
t := time.Now()
interceptWriter := stResponseWriter{w, 0, 0}
handler.ServeHTTP(&interceptWriter, r)
log.Printf("%s - - %s \"%s %s %s\" %d %d %s %dus\n",
r.RemoteAddr,
t.Format("02/Jan/2006:15:04:05 -0700"),
r.Method,
r.URL.Path,
r.Proto,
interceptWriter.HTTPStatus,
interceptWriter.ResponseSize,
r.UserAgent(),
time.Since(t),
)
})
}