4 // LoggingResponseWriter
7 "github.com/gorilla/mux"
13 type LoggingResponseWriter struct {
20 func (loggingWriter *LoggingResponseWriter) WriteHeader(code int) {
21 loggingWriter.Status = code
22 loggingWriter.ResponseWriter.WriteHeader(code)
25 func (loggingWriter *LoggingResponseWriter) Write(data []byte) (int, error) {
26 loggingWriter.Length += len(data)
27 if loggingWriter.Status >= 400 {
28 loggingWriter.Response += string(data)
30 return loggingWriter.ResponseWriter.Write(data)
33 type LoggingRESTRouter struct {
37 func MakeLoggingRESTRouter() *LoggingRESTRouter {
38 router := MakeRESTRouter()
39 return (&LoggingRESTRouter{router})
42 func (loggingRouter *LoggingRESTRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
43 loggingWriter := LoggingResponseWriter{200, 0, resp, ""}
44 loggingRouter.router.ServeHTTP(&loggingWriter, req)
45 if loggingWriter.Status >= 400 {
46 log.Printf("[%s] %s %s %d %d \"%s\"", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.Status, loggingWriter.Length, strings.TrimSpace(loggingWriter.Response))
48 log.Printf("[%s] %s %s %d %d \"OK\"", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.Status, loggingWriter.Length)