Skip to content

Instantly share code, notes, and snippets.

@gsingharoy
Created September 30, 2017 16:17
Show Gist options
  • Save gsingharoy/6a10ec0d827c142d719311256a646d52 to your computer and use it in GitHub Desktop.
Save gsingharoy/6a10ec0d827c142d719311256a646d52 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
)
// A Logger function which simply wraps the handler function around some log messages
func Logger(fn func(w http.ResponseWriter, r *http.Request, param httprouter.Params)) func(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
start := time.Now()
log.Printf("%s %s", r.Method, r.URL.Path)
fn(w, r, param)
log.Printf("Done in %v (%s %s)", time.Since(start), r.Method, r.URL.Path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment