Package log provides a handler that logs each request/response (time, duration, status, method, path).
The log formatting can either be couloured or not.
-
Get package:
go get -u github.com/gowww/log
-
Import it in your code:
import "github.com/gowww/log"
To wrap an http.Handler, use Handle:
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
})
http.ListenAndServe(":8080", log.Handle(mux, nil))
To wrap an http.HandlerFunc, use HandleFunc:
http.Handle("/", log.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
}, nil))
http.ListenAndServe(":8080", nil)
All in all, make sure to include this handler above any other handler to get accurate performance logs.
If you are on a Unix-based OS, you can get a colorized output:
log.Handle(handler, &log.Options{
Color: true,
})