fix(common): show exception with remote addr and duration

This commit is contained in:
ioito
2023-04-11 13:44:06 +08:00
parent 7a433c22ce
commit 014c19b24f
+11 -11
View File
@@ -240,7 +240,6 @@ func genRequestId(w http.ResponseWriter, r *http.Request) string {
}
func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// log.Printf("defaultHandler %s %s", r.Method, r.URL.Path)
rid := genRequestId(w, r)
w.Header().Set("X-Request-Host-Id", app.hostId)
lrw := &loggingResponseWriter{ResponseWriter: w, status: http.StatusOK, data: []byte{}}
@@ -256,9 +255,6 @@ func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
counter = &hi.counter4XX
} else {
counter = &hi.counter5XX
if app.exception != nil {
app.exception(r.Method, r.URL.String(), params.Body, errors.Errorf(string(lrw.data)))
}
}
duration := float64(time.Since(start).Nanoseconds()) / 1000000
counter.hit += 1
@@ -271,16 +267,20 @@ func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if hi.skipLog {
skipLog = true
}
peerServiceName := r.Header.Get("X-Yunion-Peer-Service-Name")
var remote string
if len(peerServiceName) > 0 {
remote = fmt.Sprintf("%s:%s", r.RemoteAddr, peerServiceName)
} else {
remote = r.RemoteAddr
}
if !skipLog {
peerServiceName := r.Header.Get("X-Yunion-Peer-Service-Name")
var remote string
if len(peerServiceName) > 0 {
remote = fmt.Sprintf("%s:%s", r.RemoteAddr, peerServiceName)
} else {
remote = r.RemoteAddr
}
log.Infof("%s %d %s %s %s (%s) %.2fms", app.hostId, lrw.status, rid, r.Method, r.URL, remote, duration)
}
if lrw.status >= 500 && app.exception != nil {
url := fmt.Sprintf("%d %s (%s) %.2fms", lrw.status, r.URL.String(), remote, duration)
app.exception(r.Method, url, params.Body, errors.Errorf(string(lrw.data)))
}
}
func (app *Application) handleCORS(w http.ResponseWriter, r *http.Request) bool {