-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
76 lines (67 loc) · 1.8 KB
/
handler.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package log
import (
"bufio"
"fmt"
"net"
"net/http"
"regexp"
"time"
)
// DefaultPathLogBlacklist is a basic set of paths to ignore for logging
var DefaultPathLogBlacklist = regexp.MustCompile(`/ping|/healthz`)
type statusWriter struct {
http.ResponseWriter
status int
body string
}
func (w *statusWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *statusWriter) Write(b []byte) (int, error) {
w.body = string(b)
return w.ResponseWriter.Write(b)
}
func (w *statusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := w.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, fmt.Errorf("hijacking not supported")
}
// HTTPHandler returns a handler that will log out request data.
//
// If the logger is nil, the default "main" logger is
// used.
//
// blacklist can be nil, in which case all calls are logged
func HTTPHandler(h http.Handler, logger *Logger, blacklist *regexp.Regexp) http.Handler {
if logger == nil {
logger = defaultLogger
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := &statusWriter{
ResponseWriter: w,
status: 200,
}
h.ServeHTTP(sw, r)
// get the diff and parse that time
diff := time.Now().Sub(start)
// don't log for certain paths
if blacklist != nil && blacklist.MatchString(r.URL.Path) {
return
}
diffStr := diff.String()
if diff > time.Second {
diffStr = diff.Truncate(time.Millisecond).String()
} else if diff > time.Millisecond {
diffStr = fmt.Sprintf("%0.3fms", float64(diff.Nanoseconds())/10000000.0)
}
switch c := sw.status; true {
case c >= 500:
logger.Infof("%s %s [%d] (%s)", r.Method, r.URL, c, diffStr)
default:
logger.Debugf("%s %s [%d] (%s)", r.Method, r.URL, c, diffStr)
}
})
}