forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_logger.go
53 lines (48 loc) · 1.47 KB
/
request_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
53
package buffalo
import (
"time"
humanize "github.com/dustin/go-humanize"
"github.com/gobuffalo/x/httpx"
"github.com/markbates/going/randx"
"github.com/sirupsen/logrus"
)
// RequestLogger can be be overridden to a user specified
// function that can be used to log the request.
var RequestLogger = RequestLoggerFunc
// RequestLoggerFunc is the default implementation of the RequestLogger.
// By default it will log a uniq "request_id", the HTTP Method of the request,
// the path that was requested, the duration (time) it took to process the
// request, the size of the response (and the "human" size), and the status
// code of the response.
func RequestLoggerFunc(h Handler) Handler {
return func(c Context) error {
var irid interface{}
if irid = c.Session().Get("requestor_id"); irid == nil {
irid = randx.String(10)
c.Session().Set("requestor_id", irid)
c.Session().Save()
}
rid := irid.(string) + "-" + randx.String(10)
c.Set("request_id", rid)
c.LogField("request_id", rid)
start := time.Now()
defer func() {
ws := c.Response().(*Response)
req := c.Request()
ct := httpx.ContentType(req)
if ct != "" {
c.LogField("content_type", ct)
}
c.LogFields(logrus.Fields{
"method": req.Method,
"path": req.URL.String(),
"duration": time.Since(start),
"size": ws.Size,
"human_size": humanize.Bytes(uint64(ws.Size)),
"status": ws.Status,
})
c.Logger().Info(req.URL.String())
}()
return h(c)
}
}