Helly everyone,
I’m new to Go programming. I am trying to figure out the best way to see how to enable logging inside an http handler. The way I tried was to create a struct, with a *log.Logger type as a member, and attach methods with signatures similar to http.HandlerFunc(). It works, but I’m not sure if it is the best way to do it, and if it is a good practice to share the same logger between multiple handlers thinking about the performance bottleneck it may/maynot cause. The code looks something like
type Admin struct {
L *log.Logger
}
func (a *Admin)AdminHandler (w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
a.L.Debugln(r.RequestURI)
}
func main() {
admintype := new(controller.Admin)
admintype.L = config.InitMainLogger("Handler")
r := mux.NewRouter()
r.HandleFunc("/favicon.ico", controller.FaviconHandler).Methods("GET")
r.Handle("/admin/{user:.*}", http.HandlerFunc(admintype.AdminHandler))
l.Errorln(http.ListenAndServe(conf.PORT, handlers.CombinedLoggingHandler(a, r)))
}