/
logging_middleware.go
68 lines (55 loc) · 1.67 KB
/
logging_middleware.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
package server
import (
"strconv"
"time"
"github.com/gofrs/uuid"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// ZapLogger is a middleware and zap to provide an "access log" like logging for each request.
func ZapLogger(log *zap.Logger) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
start := time.Now()
req := c.Request()
requestID := req.Header.Get(echo.HeaderXRequestID)
if requestID == "" {
requestID = uuid.Must(uuid.NewV7()).String()
req.Header.Set(echo.HeaderXRequestID, requestID)
}
err := next(c)
if err != nil {
c.Error(err)
}
stop := time.Now()
res := c.Response()
res.Header().Set(echo.HeaderXRequestID, requestID)
contentLength := req.Header.Get(echo.HeaderContentLength)
if contentLength == "" {
contentLength = "0"
}
latency := stop.Sub(start)
const preferredBase = 10
fields := []zapcore.Field{
zap.String("bytes_in", contentLength),
zap.String("bytes_out", strconv.FormatInt(res.Size, preferredBase)),
zap.String("latency", strconv.FormatInt(int64(latency), preferredBase)),
zap.Int("status", res.Status),
zap.String("host", req.Host),
zap.String("id", requestID),
zap.String("latency_human", stop.Sub(start).String()),
zap.String("method", req.Method),
zap.String("remote_ip", c.RealIP()),
zap.String("time", time.Now().Format(time.RFC3339Nano)),
zap.String("uri", req.RequestURI),
zap.String("user_agent", req.UserAgent()),
}
if err != nil {
fields = append(fields, zap.Error(err))
}
log.Info("http_request", fields...)
return nil
}
}
}