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 { func makeHandler(handler ResponseHandler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var msg string
start := time.Now() start := time.Now()
httpCode, err := handler(w, r, p) httpCode, err := handler(w, r, p)
duration := time.Since(start) duration := time.Since(start)
if err != nil { if err != nil {
stats.RecordEvent(stats.ErroredRequest) msg = err.Error()
http.Error(w, err.Error(), httpCode) } else if httpCode != http.StatusOK {
msg = http.StatusText(httpCode)
}
glog.Errorf( if len(msg) > 0 {
"Failed (%v:%s) %s with %s in %s", http.Error(w, msg, httpCode)
httpCode, stats.RecordEvent(stats.ErroredRequest)
http.StatusText(httpCode), }
r.URL.Path,
err.Error(), if len(msg) > 0 || glog.V(2) {
duration,
)
} else if glog.V(2) {
reqString := r.URL.Path reqString := r.URL.Path
if glog.V(3) { if glog.V(3) {
reqString = r.URL.RequestURI() + " for " + r.RemoteAddr reqString = r.URL.RequestURI() + " " + r.RemoteAddr
} }
glog.Infof( if len(msg) > 0 {
"Completed (%v:%s) %s in %v", glog.Errorf("[%d: %9s] %s (%s)", httpCode, duration, reqString, msg)
httpCode, } else {
http.StatusText(httpCode), glog.Infof("[%d: %9s] %s", httpCode, duration, reqString)
reqString, }
duration,
)
} }
stats.RecordEvent(stats.HandledRequest) stats.RecordEvent(stats.HandledRequest)

View file

@ -21,12 +21,11 @@ const jsonContentType = "application/json; charset=UTF-8"
func handleError(err error) (int, error) { func handleError(err error) (int, error) {
if err == nil { if err == nil {
return http.StatusOK, 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 { } else if _, ok := err.(models.ClientError); ok {
stats.RecordEvent(stats.ClientError) stats.RecordEvent(stats.ClientError)
if _, ok := err.(models.NotFoundError); ok {
return http.StatusNotFound, nil
}
return http.StatusBadRequest, nil return http.StatusBadRequest, nil
} }
return http.StatusInternalServerError, err return http.StatusInternalServerError, err