forked from code-golf/code-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
52 lines (44 loc) · 1011 Bytes
/
logger.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
package middleware
import (
"log"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
)
const (
red = "\033[31;1m"
green = "\033[32;1m"
yellow = "\033[33;1m"
blue = "\033[34;1m"
magenta = "\033[35;1m"
cyan = "\033[36;1m"
white = "\033[37;1m"
reset = "\033[0m"
)
var statusColours = [...]string{
"",
blue, // 1xx Informational response
green, // 2xx Success
cyan, // 3xx Redirection
yellow, // 4xx Client errors
red, // 5xx Server errors
}
// Logger logs each request.
func Logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
defer func() {
log.Printf(
"%s%d "+magenta+"%4s"+reset+" %s "+magenta+"%v"+white+" %s"+reset,
statusColours[ww.Status()/100],
ww.Status(),
r.Method,
r.URL.Path,
time.Since(start).Round(time.Millisecond),
r.UserAgent(),
)
}()
next.ServeHTTP(ww, r)
})
}