From 6f1d27354a39bc276c0714a58642c91280793f02 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 21 Dec 2021 11:36:01 +0800 Subject: [PATCH] chore: put error message in error.log for verbose mode (#1355) --- rest/handler/loghandler.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rest/handler/loghandler.go b/rest/handler/loghandler.go index 57d147b8..3e336b54 100644 --- a/rest/handler/loghandler.go +++ b/rest/handler/loghandler.go @@ -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() @@ -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 {