Skip to content

Instantly share code, notes, and snippets.

@jumping
Created Feb 22, 2019
Embed
What would you like to do?
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