-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_logger.go
81 lines (70 loc) · 2.08 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
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
package xecho
import (
"math"
"net/http"
"strings"
"time"
"github.com/labstack/echo"
"github.com/sirupsen/logrus"
)
type TimeProvider func() time.Time
func RequestLoggerMiddleware(timeFn TimeProvider) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return EchoHandler(func(c *Context) error { return RequestLogger(c, next, timeFn) })
}
}
func RequestLogger(c *Context, next echo.HandlerFunc, time TimeProvider) error {
request := c.Request()
if request.URL.Path == "/health" && strings.Contains(request.UserAgent(), "HealthChecker") {
return next(c)
}
before := time()
lrw := &statefulResponseWriter{ResponseWriter: c.Response().Writer}
c.Response().Writer = lrw
err := next(c)
after := time()
logger, ok := c.Logger().(*Logger)
if !ok {
c.Logger().Infof("[%s] %s %d", request.Method, c.Path(), lrw.statusCode)
return err
}
logger.
WithFields(createMap(c, after.Sub(before), lrw, err)).
Infof("[%s] %s %d", request.Method, c.Path(), lrw.statusCode)
return err
}
func createMap(c echo.Context, timeTaken time.Duration, lrw *statefulResponseWriter, err error) logrus.Fields {
r := c.Request()
fields := logrus.Fields{
"duration_ms": milliseconds(timeTaken),
"request": requestMap(r, c),
"response": responseMap(c.Response(), lrw.statusCode),
}
if err != nil {
fields["error"] = err.Error()
}
return fields
}
func milliseconds(timeTaken time.Duration) int64 {
return int64(timeTaken) / 1e6
}
func responseMap(r *echo.Response, statusCode int) logrus.Fields {
return logrus.Fields{
"status_code": statusCode,
"content_length": math.Max(float64(r.Size), 0),
}
}
func requestMap(r *http.Request, c echo.Context) logrus.Fields {
return logrus.Fields{
"method": r.Method,
"host_name": r.Host,
"query_params": c.QueryParams(),
"Content-length": math.Max(float64(r.ContentLength), 0),
"headers": logrus.Fields{
"user-agent": r.UserAgent(),
"referer": r.Referer(),
"x-forwarded-for": r.Header.Get("X-Forwarded-For"),
"x-forwarded-proto": r.Header.Get("X-Forwarded-Proto"),
},
}
}