示例#1
0
文件: panic.go 项目: sporttech/urest
func (h PanicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer func() {
		if rec := recover(); rec != nil {
			stack := make([]byte, 1024*32)
			runtime.Stack(stack, false)
			log.Printf("%v: %v\n%v", termcolor.Colorized("PANIC", termcolor.RED), rec, string(stack))
			http.Error(w, fmt.Sprintf("Server panic: %v", rec), http.StatusInternalServerError)
		}
	}()

	h.Handler.ServeHTTP(w, r)
}
示例#2
0
文件: log.go 项目: sporttech/urest
func (h *loggingHandler) log(start time.Time, tw *TransparentResponseWriter, r *http.Request) {
	dt := time.Now().Sub(start)
	dtC := termcolor.Colorized(fmt.Sprintf("%v", dt), termcolor.CYAN)

	statusC := fmt.Sprintf("%v", tw.Status)
	if tw.Success() {
		statusC = termcolor.Colorized(statusC, termcolor.GREEN)
	} else if tw.Status >= 400 {
		statusC = termcolor.Colorized(statusC, termcolor.RED)
	}

	methodC := termcolor.Colorized(r.Method, termcolor.BLUE)
	requestURIC := termcolor.Colorized(r.URL.Path, termcolor.BOLD)
	if r.URL.RawQuery != "" {
		requestURIC += "?" + r.URL.RawQuery
	}

	sizeS := ""
	if tw.Size != 0 {
		sizeS = fmt.Sprintf(", %v bytes", tw.Size)
	}

	h.l.Printf("%v %v %v %v (%v%v)", statusC, r.RemoteAddr, methodC, requestURIC, dtC, sizeS)
}