4 // LoggingResponseWriter
7 "github.com/gorilla/mux"
14 type LoggingResponseWriter struct {
21 func (loggingWriter *LoggingResponseWriter) WriteHeader(code int) {
22 loggingWriter.Status = code
23 loggingWriter.ResponseWriter.WriteHeader(code)
26 func (loggingWriter *LoggingResponseWriter) Write(data []byte) (int, error) {
27 loggingWriter.Length += len(data)
28 if loggingWriter.Status >= 400 {
29 loggingWriter.ResponseBody += string(data)
31 return loggingWriter.ResponseWriter.Write(data)
34 type LoggingRESTRouter struct {
38 func MakeLoggingRESTRouter() *LoggingRESTRouter {
39 router := MakeRESTRouter()
40 return (&LoggingRESTRouter{router})
43 func (loggingRouter *LoggingRESTRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
45 loggingWriter := LoggingResponseWriter{http.StatusOK, 0, resp, ""}
46 loggingRouter.router.ServeHTTP(&loggingWriter, req)
47 statusText := http.StatusText(loggingWriter.Status)
48 if loggingWriter.Status >= 400 {
49 statusText = strings.Replace(loggingWriter.ResponseBody, "\n", "", -1)
51 log.Printf("[%s] %s %s %.6fs %d %d \"%s\"", req.RemoteAddr, req.Method, req.URL.Path[1:], time.Since(t0).Seconds(), loggingWriter.Status, loggingWriter.Length, statusText)