forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_request.go
78 lines (73 loc) · 2.11 KB
/
log_request.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
77
78
package middleware
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/goadesign/goa"
"golang.org/x/net/context"
)
// LogRequest creates a request logger middleware.
// This middleware is aware of the RequestID middleware and if registered after it leverages the
// request ID for logging.
// If verbose is true then the middlware logs the request and response bodies.
func LogRequest(verbose bool) goa.Middleware {
return func(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
reqID := ctx.Value(reqIDKey)
if reqID == nil {
reqID = shortID()
}
ctx = goa.LogWith(ctx, "id", reqID)
startedAt := time.Now()
r := goa.ContextRequest(ctx)
goa.LogInfo(ctx, "started", r.Method, r.URL.String())
if verbose {
if len(r.Params) > 0 {
logCtx := make([]interface{}, 2*len(r.Params))
i := 0
for k, v := range r.Params {
logCtx[i] = k
logCtx[i+1] = interface{}(strings.Join(v, ", "))
i = i + 2
}
goa.LogInfo(ctx, "params", logCtx...)
}
if r.ContentLength > 0 {
if mp, ok := r.Payload.(map[string]interface{}); ok {
logCtx := make([]interface{}, 2*len(mp))
i := 0
for k, v := range mp {
logCtx[i] = k
logCtx[i+1] = interface{}(v)
i = i + 2
}
goa.LogInfo(ctx, "payload", logCtx...)
} else {
// Not the most efficient but this is used for debugging
js, err := json.Marshal(r.Payload)
if err == nil {
js = []byte("<invalid JSON>")
}
goa.LogInfo(ctx, "payload", "raw", string(js))
}
}
}
err := h(ctx, rw, req)
resp := goa.ContextResponse(ctx)
goa.LogInfo(ctx, "completed", "status", resp.Status,
"bytes", resp.Length, "time", time.Since(startedAt).String())
return err
}
}
}
// shortID produces a "unique" 6 bytes long string.
// Do not use as a reliable way to get unique IDs, instead use for things like logging.
func shortID() string {
b := make([]byte, 6)
io.ReadFull(rand.Reader, b)
return base64.StdEncoding.EncodeToString(b)
}