|
|
@ -16,6 +16,7 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/iox"
|
|
|
|
"github.com/tal-tech/go-zero/core/iox"
|
|
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/syncx"
|
|
|
|
"github.com/tal-tech/go-zero/core/timex"
|
|
|
|
"github.com/tal-tech/go-zero/core/timex"
|
|
|
|
"github.com/tal-tech/go-zero/core/utils"
|
|
|
|
"github.com/tal-tech/go-zero/core/utils"
|
|
|
|
"github.com/tal-tech/go-zero/rest/httpx"
|
|
|
|
"github.com/tal-tech/go-zero/rest/httpx"
|
|
|
@ -24,9 +25,11 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
limitBodyBytes = 1024
|
|
|
|
limitBodyBytes = 1024
|
|
|
|
slowThreshold = time.Millisecond * 500
|
|
|
|
defaultSlowThreshold = time.Millisecond * 500
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
|
|
|
|
|
|
|
|
|
|
|
type loggedResponseWriter struct {
|
|
|
|
type loggedResponseWriter struct {
|
|
|
|
w http.ResponseWriter
|
|
|
|
w http.ResponseWriter
|
|
|
|
r *http.Request
|
|
|
|
r *http.Request
|
|
|
@ -140,6 +143,11 @@ func DetailedLogHandler(next http.Handler) http.Handler {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetSlowThreshold sets the slow threshold.
|
|
|
|
|
|
|
|
func SetSlowThreshold(threshold time.Duration) {
|
|
|
|
|
|
|
|
slowThreshold.Set(threshold)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func dumpRequest(r *http.Request) string {
|
|
|
|
func dumpRequest(r *http.Request) string {
|
|
|
|
reqContent, err := httputil.DumpRequest(r, true)
|
|
|
|
reqContent, err := httputil.DumpRequest(r, true)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -155,7 +163,7 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern
|
|
|
|
logger := logx.WithContext(r.Context())
|
|
|
|
logger := logx.WithContext(r.Context())
|
|
|
|
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s - %s",
|
|
|
|
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s - %s",
|
|
|
|
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)))
|
|
|
|
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)))
|
|
|
|
if duration > slowThreshold {
|
|
|
|
if duration > slowThreshold.Load() {
|
|
|
|
logger.Slowf("[HTTP] %s - %d - %s - %s - %s - slowcall(%s)",
|
|
|
|
logger.Slowf("[HTTP] %s - %d - %s - %s - %s - slowcall(%s)",
|
|
|
|
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))
|
|
|
|
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -191,7 +199,7 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
|
|
|
|
logger := logx.WithContext(r.Context())
|
|
|
|
logger := logx.WithContext(r.Context())
|
|
|
|
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n",
|
|
|
|
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, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)))
|
|
|
|
if duration > slowThreshold {
|
|
|
|
if duration > defaultSlowThreshold {
|
|
|
|
logger.Slowf("[HTTP] %s - %d - %s - slowcall(%s)\n=> %s\n",
|
|
|
|
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, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|