-
Notifications
You must be signed in to change notification settings - Fork 34
/
logging.go
40 lines (36 loc) · 1.14 KB
/
logging.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 (
"net/http"
"time"
log "github.com/sirupsen/logrus"
"go.mozilla.org/mozlogrus"
)
func init() {
// initialize the logger
mozlogrus.Enable("autograph")
}
// logRequest is a middleware that writes details about each HTTP request processed
// but the various handlers. It is executed last to capture signing logs as well.
func logRequest() Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
// attempt to retrieve a signing registry entry for this request
// from the global sr.entry map, using mutexes
rid := getRequestID(r)
// calculate the processing time
t1 := getRequestStartTime(r)
procTs := time.Since(t1)
log.WithFields(log.Fields{
"remoteAddress": r.RemoteAddr,
"remoteAddressChain": "[" + r.Header.Get("X-Forwarded-For") + "]",
"method": r.Method,
"proto": r.Proto,
"url": r.URL.String(),
"ua": r.UserAgent(),
"rid": rid,
"t": procTs / time.Millisecond,
}).Info("request")
})
}
}