github.com/philippseith/signalr@v0.6.3/chatsample/middleware/log_requests.go (about)

     1  package middleware
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  // LogRequests writes simple request logs to STDOUT so that we can see what requests the server is handling
    10  func LogRequests(h http.Handler) http.Handler {
    11  	// type our middleware as an http.HandlerFunc so that it is seen as an http.Handler
    12  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    13  		// wrap the original response writer so we can capture response details
    14  		wrappedWriter := wrapResponseWriter(w)
    15  		start := time.Now() // request start time
    16  
    17  		// serve the inner request
    18  		h.ServeHTTP(wrappedWriter, r)
    19  
    20  		// extract request/response details
    21  		status := wrappedWriter.status
    22  		uri := r.URL.String()
    23  		method := r.Method
    24  		duration := time.Since(start)
    25  
    26  		// write to console
    27  		fmt.Printf("%03d %s %s %v\n", status, method, uri, duration)
    28  	})
    29  }