Better logging

This commit is contained in:
Justin Li 2014-07-25 03:39:02 -04:00
parent 404270b0ae
commit 3b84fb3f98
2 changed files with 21 additions and 23 deletions

View file

@ -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)

View file

@ -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