Skip to content

Instantly share code, notes, and snippets.

@jumping
Created February 22, 2019 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jumping/ab0fb37719b93327e3eb2457b1f65525 to your computer and use it in GitHub Desktop.
Save jumping/ab0fb37719b93327e3eb2457b1f65525 to your computer and use it in GitHub Desktop.
Not using context
package main
import (
"fmt"
"net/http"
"os"
"github.com/apex/log"
"github.com/gorilla/mux"
)
// routeLog perform logging on a route, returning log.Interface if want to add more specific details
func routeLog(r *http.Request, info string) *log.Entry {
l := log.WithFields(log.Fields{
"method": r.Method,
"requestURI": r.RequestURI,
})
l.Info(info)
return l
}
func index(w http.ResponseWriter, r *http.Request) {
routeLog(r, "index")
fmt.Fprintf(w, "hello")
}
func about(w http.ResponseWriter, r *http.Request) {
routeLog(r, "about")
fmt.Fprintf(w, "about")
}
func main() {
app := mux.NewRouter()
app.HandleFunc("/", index)
app.HandleFunc("/about", about)
if err := http.ListenAndServe(":"+os.Getenv("PORT"), app); err != nil {
log.WithError(err).Fatal("error listening")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment