Logging requests

Because logging is such a common task, Negroni comes with a logger middleware that you can use, as demonstrated in the following example:

package mainimport (   "fmt"   "net/http"   "github.com/urfave/negroni")// Return response to clientfunc indexHandler(writer http.ResponseWriter, request *http.Request) {   fmt.Fprintf(writer, "You requested: " + request.URL.Path)}func main() {   multiplexer := http.NewServeMux()   multiplexer.HandleFunc("/", indexHandler)   negroniHandler := negroni.New()   negroniHandler.Use(negroni.NewLogger()) // Negroni's default logger   negroniHandler.UseHandler(multiplexer)   http.ListenAndServe("localhost:3000", negroniHandler)}

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.