-
Notifications
You must be signed in to change notification settings - Fork 22
/
middleware.go
188 lines (156 loc) · 3.82 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package echologrus
import (
"io"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
)
// Logrus : implement Logger
type Logrus struct {
*logrus.Logger
}
// Logger ...
var Logger *logrus.Logger
// GetEchoLogger for e.Logger
func GetEchoLogger() Logrus {
return Logrus{Logger}
}
// Level returns logger level
func (l Logrus) Level() log.Lvl {
switch l.Logger.Level {
case logrus.DebugLevel:
return log.DEBUG
case logrus.WarnLevel:
return log.WARN
case logrus.ErrorLevel:
return log.ERROR
case logrus.InfoLevel:
return log.INFO
default:
l.Panic("Invalid level")
}
return log.OFF
}
// SetHeader is a stub to satisfy interface
// It's controlled by Logger
func (l Logrus) SetHeader(_ string) {}
// SetPrefix It's controlled by Logger
func (l Logrus) SetPrefix(s string) {}
// Prefix It's controlled by Logger
func (l Logrus) Prefix() string {
return ""
}
// SetLevel set level to logger from given log.Lvl
func (l Logrus) SetLevel(lvl log.Lvl) {
switch lvl {
case log.DEBUG:
Logger.SetLevel(logrus.DebugLevel)
case log.WARN:
Logger.SetLevel(logrus.WarnLevel)
case log.ERROR:
Logger.SetLevel(logrus.ErrorLevel)
case log.INFO:
Logger.SetLevel(logrus.InfoLevel)
default:
l.Panic("Invalid level")
}
}
// Output logger output func
func (l Logrus) Output() io.Writer {
return l.Out
}
// SetOutput change output, default os.Stdout
func (l Logrus) SetOutput(w io.Writer) {
Logger.SetOutput(w)
}
// Printj print json log
func (l Logrus) Printj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Print()
}
// Debugj debug json log
func (l Logrus) Debugj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Debug()
}
// Infoj info json log
func (l Logrus) Infoj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Info()
}
// Warnj warning json log
func (l Logrus) Warnj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Warn()
}
// Errorj error json log
func (l Logrus) Errorj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Error()
}
// Fatalj fatal json log
func (l Logrus) Fatalj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Fatal()
}
// Panicj panic json log
func (l Logrus) Panicj(j log.JSON) {
Logger.WithFields(logrus.Fields(j)).Panic()
}
// Print string log
func (l Logrus) Print(i ...interface{}) {
Logger.Print(i[0].(string))
}
// Debug string log
func (l Logrus) Debug(i ...interface{}) {
Logger.Debug(i[0].(string))
}
// Info string log
func (l Logrus) Info(i ...interface{}) {
Logger.Info(i[0].(string))
}
// Warn string log
func (l Logrus) Warn(i ...interface{}) {
Logger.Warn(i[0].(string))
}
// Error string log
func (l Logrus) Error(i ...interface{}) {
Logger.Error(i[0].(string))
}
// Fatal string log
func (l Logrus) Fatal(i ...interface{}) {
Logger.Fatal(i[0].(string))
}
// Panic string log
func (l Logrus) Panic(i ...interface{}) {
Logger.Panic(i[0].(string))
}
func logrusMiddlewareHandler(c echo.Context, next echo.HandlerFunc) error {
req := c.Request()
res := c.Response()
start := time.Now()
if err := next(c); err != nil {
c.Error(err)
}
stop := time.Now()
Logger.WithFields(map[string]interface{}{
"time_rfc3339": time.Now().Format(time.RFC3339),
"remote_ip": c.RealIP(),
"host": req.Host,
"uri": req.RequestURI,
"method": req.Method,
"path": req.URL.Path,
"referer": req.Referer(),
"user_agent": req.UserAgent(),
"status": res.Status,
"latency": stop.Sub(start).Microseconds(),
"latency_human": stop.Sub(start).String(),
"bytes_in": req.ContentLength,
"bytes_out": res.Size,
}).Info("Handled request")
return nil
}
func logger(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return logrusMiddlewareHandler(c, next)
}
}
// Hook is a function to process middleware.
func Hook() echo.MiddlewareFunc {
return logger
}