Add request method in http log (#1120)

* Add request method in http log

* Update log format
master
chenquan 3 years ago committed by GitHub
parent 426b09c356
commit 022c100dc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -152,11 +152,12 @@ func dumpRequest(r *http.Request) string {
func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *internal.LogCollector) { func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *internal.LogCollector) {
var buf bytes.Buffer var buf bytes.Buffer
duration := timer.Duration() duration := timer.Duration()
buf.WriteString(fmt.Sprintf("%d - %s - %s - %s - %s", logger := logx.WithContext(r.Context())
code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s - %s",
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)))
if duration > slowThreshold { if duration > slowThreshold {
logx.WithContext(r.Context()).Slowf("[HTTP] %d - %s - %s - %s - slowcall(%s)", logger.Slowf("[HTTP] %s - %d - %s - %s - %s - slowcall(%s)",
code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))
} }
ok := isOkResponse(code) ok := isOkResponse(code)
@ -177,9 +178,9 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern
} }
if ok { if ok {
logx.WithContext(r.Context()).Info(buf.String()) logger.Info(buf.String())
} else { } else {
logx.WithContext(r.Context()).Error(buf.String()) logger.Error(buf.String())
} }
} }
@ -187,11 +188,12 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
logs *internal.LogCollector) { logs *internal.LogCollector) {
var buf bytes.Buffer var buf bytes.Buffer
duration := timer.Duration() duration := timer.Duration()
buf.WriteString(fmt.Sprintf("%d - %s - %s\n=> %s\n", logger := logx.WithContext(r.Context())
response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))) buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n",
r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)))
if duration > slowThreshold { if duration > slowThreshold {
logx.WithContext(r.Context()).Slowf("[HTTP] %d - %s - slowcall(%s)\n=> %s\n", logger.Slowf("[HTTP] %s - %d - %s - slowcall(%s)\n=> %s\n",
response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)) r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
} }
body := logs.Flush() body := logs.Flush()
@ -204,7 +206,7 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
buf.WriteString(fmt.Sprintf("<= %s", respBuf)) buf.WriteString(fmt.Sprintf("<= %s", respBuf))
} }
logx.WithContext(r.Context()).Info(buf.String()) logger.Info(buf.String())
} }
func isOkResponse(code int) bool { func isOkResponse(code int) bool {

Loading…
Cancel
Save