This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
/
logger.go
77 lines (63 loc) · 1.59 KB
/
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
package middleware
import (
"time"
"github.com/docker/go-units"
"github.com/labstack/echo/v4"
nslog "github.com/13k/night-stalker/internal/logger"
nsstrconv "github.com/13k/night-stalker/internal/strconv"
nswebctx "github.com/13k/night-stalker/web/internal/context"
)
func Logger(logger *nslog.Logger) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := c.(*nswebctx.Context)
req := c.Request()
res := c.Response()
start := time.Now()
err := next(cc)
if cc.SkipLogging() {
return err
}
stop := time.Now()
latency := stop.Sub(start)
var requestID string
if requestID = req.Header.Get(echo.HeaderXRequestID); requestID == "" {
requestID = res.Header().Get(echo.HeaderXRequestID)
}
var bytesIn uint64
if h := req.Header.Get(echo.HeaderContentLength); h != "" {
bytesIn = nsstrconv.SafeParseUint(h)
}
l := logger.WithOFields(
"id", cc.ID(),
"status", res.Status,
"remote_ip", c.RealIP(),
"host", req.Host,
"method", req.Method,
"uri", req.RequestURI,
"user_agent", req.UserAgent(),
"latency", int64(latency),
"latency_h", latency.String(),
"rx", bytesIn,
"rx_h", units.BytesSize(float64(bytesIn)),
"tx", res.Size,
"tx_h", units.BytesSize(float64(res.Size)),
)
if requestID != "" {
l = l.WithField("request_id", requestID)
}
if err != nil {
l = l.WithError(err)
}
switch {
case res.Status >= 500:
l.Errorz()
case res.Status >= 400:
l.Warnz()
default:
l.Infoz()
}
return err
}
}
}