-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
223 lines (190 loc) · 4.34 KB
/
logging.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package xecho
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
)
func DebugLoggerMiddleware(isDebug bool) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return EchoHandler(func(c *Context) error {
if !isDebug {
return next(c)
}
drw := &debugResponseWriter{w: c.Response().Writer}
c.Response().Writer = drw
dumpRequest(c)
err := next(c)
dumpResponse(c, drw)
return err
})
}
}
type debugResponseWriter struct {
isDebug bool
w http.ResponseWriter
statusCode int
body bytes.Buffer
}
func (drw *debugResponseWriter) Header() http.Header {
return drw.w.Header()
}
func (drw *debugResponseWriter) Write(b []byte) (int, error) {
_, _ = drw.body.Write(b)
return drw.w.Write(b)
}
func (drw *debugResponseWriter) WriteHeader(code int) {
drw.statusCode = code
drw.w.WriteHeader(code)
}
func dumpRequest(c *Context) {
reqDump, err := httputil.DumpRequest(c.Request(), true)
if err == nil {
c.Logger().Debugf("%s", string(reqDump))
}
}
func dumpResponse(c *Context, drw *debugResponseWriter) {
res := &http.Response{
Body: ioutil.NopCloser(&drw.body),
Header: drw.Header(),
StatusCode: drw.statusCode,
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: int64(drw.body.Len()),
}
body := res.Header.Get("Content-Type") != "text/html"
resDump, err := httputil.DumpResponse(res, body)
if err == nil {
c.Logger().Debugf("%s", string(resDump))
}
}
func requestScopeLogger(
logger *logrus.Entry,
r *http.Request,
route string,
ip string,
correlationID string,
) *Logger {
ctxLogger := logger.WithFields(logrus.Fields{
"correlation_id": correlationID,
"url": r.RequestURI,
"route": route,
"remote_addr": r.RemoteAddr,
"method": r.Method,
"ip": ip,
"headers": logrus.Fields{
"host": r.Host,
"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"),
},
})
return &Logger{ctxLogger}
}
// Wrap logrus entry and implement additional methods required to
// satisfy echo logger interface
type Logger struct {
*logrus.Entry
}
func (l *Logger) Output() io.Writer {
return l.Logger.Out
}
func (l *Logger) SetOutput(w io.Writer) {
l.Logger.Out = w
}
func (l *Logger) Prefix() string {
return "" // not implemented - only added for API compatibility with echo logger
}
func (l *Logger) SetPrefix(_ string) {
// not implemented - only added for API compatibility with echo logger
}
func (l *Logger) Level() log.Lvl {
return logrusLeveltoEchoLevel(l.Logger.Level)
}
func (l *Logger) SetLevel(lvl log.Lvl) {
l.Logger.Level = echoLeveltoLogrusLevel(lvl)
}
func (l *Logger) SetHeader(_ string) {
// not implemented - only added for API compatibility with echo logger
}
func (l *Logger) Printj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Println(string(b))
}
func (l *Logger) Debugj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Debugln(string(b))
}
func (l *Logger) Infoj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Infoln(string(b))
}
func (l *Logger) Warnj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Warnln(string(b))
}
func (l *Logger) Errorj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Errorln(string(b))
}
func (l *Logger) Fatalj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Fatalln(string(b))
}
func (l *Logger) Panicj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Panicln(string(b))
}
func echoLeveltoLogrusLevel(level log.Lvl) logrus.Level {
switch level {
case log.DEBUG:
return logrus.DebugLevel
case log.INFO:
return logrus.InfoLevel
case log.WARN:
return logrus.WarnLevel
case log.ERROR:
return logrus.ErrorLevel
}
return logrus.InfoLevel
}
func logrusLeveltoEchoLevel(level logrus.Level) log.Lvl {
switch level {
case logrus.DebugLevel:
return log.DEBUG
case logrus.InfoLevel:
return log.INFO
case logrus.WarnLevel:
return log.WARN
case logrus.ErrorLevel:
return log.ERROR
}
return log.OFF
}