Better logging
This commit is contained in:
parent
404270b0ae
commit
3b84fb3f98
2 changed files with 21 additions and 23 deletions
37
http/http.go
37
http/http.go
|
@ -28,35 +28,34 @@ type Server struct {
|
|||
|
||||
func makeHandler(handler ResponseHandler) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
var msg string
|
||||
|
||||
start := time.Now()
|
||||
httpCode, err := handler(w, r, p)
|
||||
duration := time.Since(start)
|
||||
|
||||
if err != nil {
|
||||
stats.RecordEvent(stats.ErroredRequest)
|
||||
http.Error(w, err.Error(), httpCode)
|
||||
msg = err.Error()
|
||||
} else if httpCode != http.StatusOK {
|
||||
msg = http.StatusText(httpCode)
|
||||
}
|
||||
|
||||
glog.Errorf(
|
||||
"Failed (%v:%s) %s with %s in %s",
|
||||
httpCode,
|
||||
http.StatusText(httpCode),
|
||||
r.URL.Path,
|
||||
err.Error(),
|
||||
duration,
|
||||
)
|
||||
} else if glog.V(2) {
|
||||
if len(msg) > 0 {
|
||||
http.Error(w, msg, httpCode)
|
||||
stats.RecordEvent(stats.ErroredRequest)
|
||||
}
|
||||
|
||||
if len(msg) > 0 || glog.V(2) {
|
||||
reqString := r.URL.Path
|
||||
if glog.V(3) {
|
||||
reqString = r.URL.RequestURI() + " for " + r.RemoteAddr
|
||||
reqString = r.URL.RequestURI() + " " + r.RemoteAddr
|
||||
}
|
||||
|
||||
glog.Infof(
|
||||
"Completed (%v:%s) %s in %v",
|
||||
httpCode,
|
||||
http.StatusText(httpCode),
|
||||
reqString,
|
||||
duration,
|
||||
)
|
||||
if len(msg) > 0 {
|
||||
glog.Errorf("[%d: %9s] %s (%s)", httpCode, duration, reqString, msg)
|
||||
} else {
|
||||
glog.Infof("[%d: %9s] %s", httpCode, duration, reqString)
|
||||
}
|
||||
}
|
||||
|
||||
stats.RecordEvent(stats.HandledRequest)
|
||||
|
|
|
@ -21,12 +21,11 @@ const jsonContentType = "application/json; charset=UTF-8"
|
|||
func handleError(err error) (int, error) {
|
||||
if err == nil {
|
||||
return http.StatusOK, nil
|
||||
} else if _, ok := err.(models.NotFoundError); ok {
|
||||
stats.RecordEvent(stats.ClientError)
|
||||
return http.StatusNotFound, nil
|
||||
} else if _, ok := err.(models.ClientError); ok {
|
||||
stats.RecordEvent(stats.ClientError)
|
||||
|
||||
if _, ok := err.(models.NotFoundError); ok {
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
return http.StatusBadRequest, nil
|
||||
}
|
||||
return http.StatusInternalServerError, err
|
||||
|
|
Loading…
Reference in a new issue