/
log.go
92 lines (80 loc) · 1.99 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
87
88
89
90
91
92
package filter
import (
"strconv"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/silentred/echorus"
"github.com/silentred/toolkit/util"
"github.com/sirupsen/logrus"
)
type (
// LoggerConfig defines the config for Logger middleware.
LoggerConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
Logger util.Logger
Format logrus.Formatter
}
)
func NewConfig(logger util.Logger) LoggerConfig {
return LoggerConfig{
Skipper: DefaultSkipper,
Logger: logger,
Format: echorus.TextFormat,
}
}
// Logger returns a middleware that logs HTTP requests.
func Logger(logger util.Logger) echo.MiddlewareFunc {
return LoggerWithConfig(NewConfig(logger))
}
// LoggerWithConfig returns a Logger middleware with config.
func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultSkipper
}
if config.Format != nil {
if l, ok := config.Logger.(*echorus.Echorus); ok {
l.SetFormat(config.Format)
}
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
if config.Skipper(c) {
return next(c)
}
req := c.Request()
res := c.Response()
start := time.Now()
if err = next(c); err != nil {
c.Error(err)
}
stop := time.Now()
p := req.URL.Path
if p == "" {
p = "/"
}
cl := req.Header.Get(echo.HeaderContentLength)
if cl == "" {
cl = "0"
}
json := log.JSON{
"time_unix": strconv.FormatInt(time.Now().Unix(), 10),
"remote_ip": c.RealIP(),
"host": req.Host,
"uri": req.RequestURI,
"method": req.Method,
"path": p,
"user_agent": req.UserAgent(),
"status": res.Status,
"latency": strconv.FormatInt(int64(stop.Sub(start)), 10),
"latency_str": stop.Sub(start).String(),
"bytes_in": cl,
"bytes_out": strconv.FormatInt(res.Size, 10),
}
config.Logger.Infoj(json)
return
}
}
}