4 // LoggingResponseWriter
8 "github.com/gorilla/mux"
13 type LoggingResponseWriter struct {
19 func (loggingWriter *LoggingResponseWriter) WriteHeader(code int) {
20 loggingWriter.Status = code
21 loggingWriter.ResponseWriter.WriteHeader(code)
24 func (loggingWriter *LoggingResponseWriter) Write(data []byte) (int, error){
25 loggingWriter.Data.Write(data)
26 return loggingWriter.ResponseWriter.Write(data)
29 type LoggingRESTRouter struct {
33 func MakeLoggingRESTRouter() (*LoggingRESTRouter) {
34 router := MakeRESTRouter()
35 return (&LoggingRESTRouter{router})
38 func (loggingRouter *LoggingRESTRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
39 loggingWriter := LoggingResponseWriter{200, bytes.NewBuffer(make([]byte, 0, 0)), resp}
40 loggingRouter.router.ServeHTTP(&loggingWriter, req)
41 if loggingWriter.Status == 200 {
42 if loggingWriter.Data.Len() > 200 { // could be large block, so just print the size
43 log.Printf("[%s] %s %s %d %d", req.RemoteAddr, req.Method, req.URL.Path[1:],
44 loggingWriter.Status, loggingWriter.Data.Len())
45 } else { // this could be a hash or status or a small block etc
46 log.Printf("[%s] %s %s %d %s", req.RemoteAddr, req.Method, req.URL.Path[1:],
47 loggingWriter.Status, loggingWriter.Data)
50 log.Printf("[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.Status)