google app engine - Go wrap httprouter to have logger? -
i have next httprouter
handler want customize can inject own logger functions.
router := httprouter.new() router.handle("get", "/mysite", mylogger(handler1))
and mylogger
like:
var logger = log.new(os.stdout, "[log] ", 0) func mylogger(fn func(w http.responsewriter, r *http.request, param httprouter.params)) func(w http.responsewriter, r *http.request, param httprouter.params) { homecoming func(w http.responsewriter, r *http.request, param httprouter.params) { start := time.now() logger.printf("%s %s", r.method, r.url.path) fn(w, r, param) logger.printf("done in %v (%s %s)", time.since(start), r.method, r.url.path) } }
and myhandler
like:
func myhandler(w http.responsewriter, r *http.request, _ httprouter.params) { .... }
is there way can wrap httprouter
handler not have pass handler mylogger
function? want in go appengine context:
func appenginehandler(c appengine.context, w http.responsewriter, r *http.request, _ httprouter.params) { ... c.infof("here's log") }
without having used httprouter
bundle before, appear need alter function signature you're attempting wrap.
so, firstly, alter declaration of fn
argument in method:
func mylogger(fn func(c *myapp.context, w http.responsewriter, r *http.request, param httprouter.params)) // ^^^^^ have take context of own
..then create , pass context handler in wrapper:
c := &myapp.context{ your: arguments, here: true } fn(c, w, r, param) // ^^ - pass context in
then alter signature of handler:
func myhandler(c *myapp.context, w http.responsewriter, r *http.request, _ httprouter.params) { // utilize c here }
google-app-engine http logging go
No comments:
Post a Comment