-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
log.go
86 lines (77 loc) Β· 2.53 KB
/
log.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
79
80
81
82
83
84
85
86
package middleware
import (
"context"
"net"
"net/http"
"time"
"goa.design/goa/v3/middleware"
)
// Log returns a middleware that logs incoming HTTP requests and outgoing
// responses. The middleware uses the request ID set by the RequestID middleware
// or creates a short unique request ID if missing for each incoming request and
// logs it with the request and corresponding response details.
//
// The middleware logs the incoming requests HTTP method and path as well as the
// originator of the request. The originator is computed by looking at the
// X-Forwarded-For HTTP header or - absent of that - the originating IP. The
// middleware also logs the response HTTP status code, body length (in bytes) and
// timing information.
//
// Deprecated: use OpenTelemetry instead, see for example
// github.com/goadesign/clue. This function will be removed in a future version
// of Goa.
func Log(l middleware.Logger) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log(l, r, w, h)
})
}
}
// LogContext returns a middleware that logs the incoming requests similarly to
// Log. LogContext calls the given function with the request context to extract
// the logger.
//
// Deprecated: use OpenTelemetry instead, see for example
// github.com/goadesign/clue. This function will be removed in a future version
// of Goa.
func LogContext(logFromCtx func(context.Context) middleware.Logger) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := logFromCtx(r.Context())
if l == nil {
h.ServeHTTP(w, r)
return
}
log(l, r, w, h)
})
}
}
// log does the actual logging given the logger.
func log(l middleware.Logger, r *http.Request, w http.ResponseWriter, next http.Handler) {
reqID := r.Context().Value(middleware.RequestIDKey)
if reqID == nil {
reqID = shortID()
}
started := time.Now()
l.Log("id", reqID, // nolint: errcheck
"req", r.Method+" "+r.URL.String(),
"from", from(r))
rw := CaptureResponse(w)
next.ServeHTTP(rw, r)
l.Log("id", reqID, // nolint: errcheck
"status", rw.StatusCode,
"bytes", rw.ContentLength,
"time", time.Since(started).String())
}
// from makes a best effort to compute the request client IP.
func from(req *http.Request) string {
if f := req.Header.Get("X-Forwarded-For"); f != "" {
return f
}
f := req.RemoteAddr
ip, _, err := net.SplitHostPort(f)
if err != nil {
return f
}
return ip
}