#Middleware issue

3 messages · Page 1 of 1 (latest)

lofty nest
#

I'm trying to write a logging middleware for a simple http server. I want to log the response status and bytes written, which is why I followed https://stackoverflow.com/questions/53272536/how-do-i-get-response-statuscode-in-golang-middleware to wrap the ResponseWriter like so:

import (
    "github.com/urfave/negroni"
)

func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
        log.Printf("--> %s %s", req.Method, req.URL.Path)

        lrw := negroni.NewResponseWriter(w)
        wrappedHandler.ServeHTTP(lrw, req)

        statusCode := lrw.Status()
        log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
    })
}```

Now the problem is I'd also like to log some context variables set by middleware that come after logger in the chain. They're set on r.Context() but since the r here is the older request before the auth middleware added extra info, I have no way of accessing it
warm bridge
#

What kind of things are set inside the context?

lofty nest
#

just a few string variables id like to access in my logs