Skip to content

Instantly share code, notes, and snippets.

@tonespy
Created June 23, 2019 08:18
Show Gist options
  • Save tonespy/354953e3102b37498a0ca5273aac0ae0 to your computer and use it in GitHub Desktop.
Save tonespy/354953e3102b37498a0ca5273aac0ae0 to your computer and use it in GitHub Desktop.
API Logger
package app
import (
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
)
// Logger :- An helper function for logging
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