I am using the following for a simple server. I am wondering how to setup an access log for all the requests logging the timestamp, method, request url and the http response code.
http.HandleFunc("/foo", funcFoo)
err := http.ListenAndServe("127.0.0.1:2074", nil)
Take a look here: http://github.com/gorilla/handlers
http.Handle("/foo", funcFoo)
err := http.ListenAndServe("127.0.0.1:2074", handlers.LoggingHandler(os.Stdout, http.DefaultServeMux))
This will log any incoming connections across the whole server. os.Stdout
can be replaced by anything that provides an io.Writer
(i.e. a file, a HTTP stream, etc). If you want it to be per-route, you can do:
http.Handle("/foo", handlers.LoggingHandler(os.Stdout, funcFoo))
It will also work with gorilla/mux and other routers/frameworks that are http.Handler
compatible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With