blob: 709666b78a433713c86161917d0f5ef58f93988c (
plain)
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
|
package main
import (
"log"
"net/http"
"time"
)
// Logging middleware
func Logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
// Start buffered write
bw := new(Buffer)
next.ServeHTTP(bw, r)
// Write out the buffer
size, _ := bw.Apply(w)
// Usually milliseconds
latency := time.Since(startTime).Seconds() * 1000
log.Printf("%s %s %d %d (%.1fms)\n", r.Method, r.URL.Path, bw.Status, size, latency)
})
}
|