We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Logging Requests

It's very common to log requests in a web service. One of the cleaner ways to implement this is with a middleware function that logs the request after it's been served:

func requestLogger(logger *log.Logger) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			next.ServeHTTP(w, r)
			logger.Printf("Wake up babe, a new %s request to %s just dropped", r.Method, r.URL.Path)
		})
	}
}

This one simply logs the request method and path after the request has been served. Notice that it takes a *log.Logger as an argument, allowing you to use any logger you want on a per-handler basis. So, instead of declaring a handler that we want to log like this:

mux.HandleFunc("POST /api/shorten", apiCfg.handlerShortenURL)

We can use middleware:

mux.Handle("/api/shorten", requestLogger(logger)(http.HandlerFunc(apiCfg.handlerShortenURL)))

Alternatively, we can wrap the entire mux with the middleware, so that all requests are logged:

srv = &http.Server{
	Addr:    fmt.Sprintf(":%d", port),
	Handler: requestLogger(logger)(mux),
}

Assignment

Log each served request with middleware.

  1. Served request: METHOD Path
    
    Where METHOD is the HTTP method of the request, and Path is the path of the request. For example:
    Served request: GET /
    
  2. go run . 2>&1 | sh -c 'trap "" INT; tee linko.out.log'
    

Run and submit the CLI tests from the root of the Linko repo.