@@ -27,16 +27,17 @@ func RequestLogging(config *Config) func(http.Handler) http.Handler {
27
27
reserve .LastHandling (wrw )
28
28
}()
29
29
30
- next .ServeHTTP (wrw , r )
30
+ next .ServeHTTP (wrw , reserve . request )
31
31
}
32
+
32
33
return http .HandlerFunc (fn )
33
34
}
34
35
}
35
36
36
37
// RequestLoggingWithEcho creates the middleware which logs a request log and creates a request-context logger
37
38
func RequestLoggingWithEcho (config * Config ) echo.MiddlewareFunc {
38
39
return func (next echo.HandlerFunc ) echo.HandlerFunc {
39
- fn := func (c echo.Context ) error {
40
+ return func (c echo.Context ) error {
40
41
reserve := NewReserve (config , c .Request ())
41
42
42
43
wrw := & wrappedResponseWriter {
@@ -53,7 +54,6 @@ func RequestLoggingWithEcho(config *Config) echo.MiddlewareFunc {
53
54
54
55
return next (c )
55
56
}
56
- return fn
57
57
}
58
58
}
59
59
@@ -99,7 +99,7 @@ func NewReserve(config *Config, r *http.Request) *Reserve {
99
99
loggedSeverity : make ([]Severity , 0 , 10 ),
100
100
Skip : config .Skip ,
101
101
}
102
- ctx := context .WithValue (r .Context (), contextLoggerKey , contextLogger )
102
+ ctx := context .WithValue (r .Context (), ContextLoggerKey , contextLogger )
103
103
104
104
return & Reserve {
105
105
before : before ,
@@ -159,15 +159,15 @@ func (w *wrappedResponseWriter) Write(b []byte) (int, error) {
159
159
return n , err
160
160
}
161
161
162
- type HttpRequest struct {
162
+ type HTTPRequest struct {
163
163
RequestMethod string `json:"requestMethod"`
164
164
RequestUrl string `json:"requestUrl"`
165
165
RequestSize string `json:"requestSize"`
166
166
Status int `json:"status"`
167
167
ResponseSize string `json:"responseSize"`
168
168
UserAgent string `json:"userAgent"`
169
- RemoteIp string `json:"remoteIp"`
170
- ServerIp string `json:"serverIp"`
169
+ RemoteIP string `json:"remoteIp"`
170
+ ServerIP string `json:"serverIp"`
171
171
Referer string `json:"referer"`
172
172
Latency string `json:"latency"`
173
173
CacheLookup bool `json:"cacheLookup"`
@@ -176,28 +176,28 @@ type HttpRequest struct {
176
176
Protocol string `json:"protocol"`
177
177
}
178
178
179
- type HttpRequestLog struct {
179
+ type HTTPRequestLog struct {
180
180
Time string `json:"time"`
181
181
Trace string `json:"logging.googleapis.com/trace"`
182
182
Severity string `json:"severity"`
183
- HttpRequest HttpRequest `json:"httpRequest"`
183
+ HTTPRequest HTTPRequest `json:"httpRequest"`
184
184
AdditionalData AdditionalData `json:"data,omitempty"`
185
185
}
186
186
187
187
func writeRequestLog (r * http.Request , config * Config , status int , responseSize int , elapsed time.Duration , trace string , severity Severity ) error {
188
- requestLog := & HttpRequestLog {
188
+ requestLog := & HTTPRequestLog {
189
189
Time : time .Now ().Format (time .RFC3339Nano ),
190
190
Trace : trace ,
191
191
Severity : severity .String (),
192
- HttpRequest : HttpRequest {
192
+ HTTPRequest : HTTPRequest {
193
193
RequestMethod : r .Method ,
194
194
RequestUrl : r .URL .RequestURI (),
195
195
RequestSize : fmt .Sprintf ("%d" , r .ContentLength ),
196
196
Status : status ,
197
197
ResponseSize : fmt .Sprintf ("%d" , responseSize ),
198
198
UserAgent : r .UserAgent (),
199
- RemoteIp : getRemoteIp (r ),
200
- ServerIp : getServerIp (),
199
+ RemoteIP : getRemoteIP (r ),
200
+ ServerIP : getServerIP (),
201
201
Referer : r .Referer (),
202
202
Latency : fmt .Sprintf ("%fs" , elapsed .Seconds ()),
203
203
CacheLookup : false ,
@@ -207,31 +207,36 @@ func writeRequestLog(r *http.Request, config *Config, status int, responseSize i
207
207
},
208
208
AdditionalData : config .AdditionalData ,
209
209
}
210
- requestLogJson , err := json .Marshal (requestLog )
210
+
211
+ jsonByte , err := json .Marshal (requestLog )
211
212
if err != nil {
212
213
return err
213
214
}
214
- requestLogJson = append (requestLogJson , '\n' )
215
215
216
- _ , err = config .RequestLogOut .Write (requestLogJson )
216
+ // append \n
217
+ jsonByte = append (jsonByte , 0xa )
218
+
219
+ _ , err = config .RequestLogOut .Write (jsonByte )
217
220
return err
218
221
}
219
222
220
- func getRemoteIp (r * http.Request ) string {
223
+ func getRemoteIP (r * http.Request ) string {
221
224
parts := strings .Split (r .RemoteAddr , ":" )
222
225
return parts [0 ]
223
226
}
224
227
225
- func getServerIp () string {
228
+ func getServerIP () string {
226
229
ifaces , err := net .Interfaces ()
227
230
if err != nil {
228
231
return ""
229
232
}
233
+
230
234
for _ , i := range ifaces {
231
235
addrs , err := i .Addrs ()
232
236
if err != nil {
233
237
return ""
234
238
}
239
+
235
240
for _ , addr := range addrs {
236
241
if ipnet , ok := addr .(* net.IPNet ); ok && ! ipnet .IP .IsLoopback () {
237
242
if ipnet .IP .To4 () != nil {
@@ -240,5 +245,6 @@ func getServerIp() string {
240
245
}
241
246
}
242
247
}
248
+
243
249
return ""
244
250
}
0 commit comments