Skip to content

Commit

Permalink
chore: put error message in error.log for verbose mode (zeromicro#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Dec 21, 2021
1 parent 2610173 commit 6f1d273
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions rest/handler/loghandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,13 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
logs *internal.LogCollector) {
var buf bytes.Buffer
duration := timer.Duration()
code := response.writer.code
logger := logx.WithContext(r.Context())
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n",
r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)))
r.Method, code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)))
if duration > defaultSlowThreshold {
logger.Slowf("[HTTP] %s - %d - %s - slowcall(%s)\n=> %s\n",
r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
r.Method, code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
}

body := logs.Flush()
Expand All @@ -214,7 +215,11 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
buf.WriteString(fmt.Sprintf("<= %s", respBuf))
}

logger.Info(buf.String())
if isOkResponse(code) {
logger.Info(buf.String())
} else {
logger.Error(buf.String())
}
}

func isOkResponse(code int) bool {
Expand Down

0 comments on commit 6f1d273

Please sign in to comment.