feat: logx with color (#1872)
* feat: logx with color * chore: update logs * fix test error * chore: change colors of http codes * chore: add comments * chore: use faith/color instead of ascii code color * chore: update colors * chore: update colors * chore: fix duplicated slowcall text * chore: remove slowcall colors
This commit is contained in:
@@ -11,9 +11,11 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
"github.com/zeromicro/go-zero/core/iox"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/syncx"
|
||||
@@ -157,15 +159,21 @@ func dumpRequest(r *http.Request) string {
|
||||
return string(reqContent)
|
||||
}
|
||||
|
||||
func isOkResponse(code int) bool {
|
||||
// not server error
|
||||
return code < http.StatusInternalServerError
|
||||
}
|
||||
|
||||
func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *internal.LogCollector) {
|
||||
var buf bytes.Buffer
|
||||
duration := timer.Duration()
|
||||
logger := logx.WithContext(r.Context()).WithDuration(duration)
|
||||
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s",
|
||||
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()))
|
||||
buf.WriteString(fmt.Sprintf("[HTTP] %s - %s %s - %s - %s",
|
||||
wrapStatusCode(code), wrapMethod(r.Method), r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()))
|
||||
if duration > slowThreshold.Load() {
|
||||
logger.Slowf("[HTTP] %s - %d - %s - %s - %s - slowcall(%s)",
|
||||
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))
|
||||
logger.Slowf("[HTTP] %s - %s - %s %s - %s - slowcall(%s)",
|
||||
wrapStatusCode(code), wrapMethod(r.Method), r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(),
|
||||
fmt.Sprintf("slowcall(%s)", timex.ReprOfDuration(duration)))
|
||||
}
|
||||
|
||||
ok := isOkResponse(code)
|
||||
@@ -201,8 +209,8 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
|
||||
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n",
|
||||
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, code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
|
||||
logger.Slowf("[HTTP] %s - %d - %s - slowcall(%s)\n=> %s\n", r.Method, code, r.RemoteAddr,
|
||||
fmt.Sprintf("slowcall(%s)", timex.ReprOfDuration(duration)), dumpRequest(r))
|
||||
}
|
||||
|
||||
body := logs.Flush()
|
||||
@@ -222,7 +230,44 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
|
||||
}
|
||||
}
|
||||
|
||||
func isOkResponse(code int) bool {
|
||||
// not server error
|
||||
return code < http.StatusInternalServerError
|
||||
func wrapMethod(method string) string {
|
||||
var colour color.Color
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
colour = color.BgBlue
|
||||
case http.MethodPost:
|
||||
colour = color.BgCyan
|
||||
case http.MethodPut:
|
||||
colour = color.BgYellow
|
||||
case http.MethodDelete:
|
||||
colour = color.BgRed
|
||||
case http.MethodPatch:
|
||||
colour = color.BgGreen
|
||||
case http.MethodHead:
|
||||
colour = color.BgMagenta
|
||||
case http.MethodOptions:
|
||||
colour = color.BgWhite
|
||||
}
|
||||
|
||||
if colour == color.NoColor {
|
||||
return method
|
||||
}
|
||||
|
||||
return logx.WithColorPadding(method, colour)
|
||||
}
|
||||
|
||||
func wrapStatusCode(code int) string {
|
||||
var colour color.Color
|
||||
switch {
|
||||
case code >= http.StatusOK && code < http.StatusMultipleChoices:
|
||||
colour = color.BgGreen
|
||||
case code >= http.StatusMultipleChoices && code < http.StatusBadRequest:
|
||||
colour = color.BgBlue
|
||||
case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
|
||||
colour = color.BgMagenta
|
||||
default:
|
||||
colour = color.BgYellow
|
||||
}
|
||||
|
||||
return logx.WithColorPadding(strconv.Itoa(code), colour)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user