mantzas
(Mantzas)
November 4, 2015, 7:55pm
1
Hi,
i am relatively new to go.
i have written a logging middle-ware in order log some stuff.
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
next.ServeHTTP(w, r)
log.WithFields(log.Fields{"method": r.Method, "route": r.URL.String(), "status": "???", "time": time.Since(startTime)}).Debugln()
})
}
I am trying to get the status from the http.Responsewriter.
Do i have to cast the writer to something in order to achieve this?
mantzas
(Mantzas)
November 4, 2015, 10:39pm
3
@netixen how do i get from w http.ResponseWriter -> Response -> w.Status? like i said i am new and there is a missing link between them.
Bjoern
(Björn Graf)
November 4, 2015, 10:57pm
4
As far as I can tell you cannot and you have to implement the ResponseWriter interface to wrap the original one to intercept the stuff you’re interested in.
The Gorilla project has a handler package with a logging handler that demonstrates this at https://github.com/gorilla/handlers/blob/master/handlers.go#L102
1 Like
netixen
(netixen)
November 5, 2015, 1:12am
5
@Bjoern is correct, the response I was thinking of is what you get on the client side.
mantzas
(Mantzas)
November 5, 2015, 7:21am
6
@Bjoern it seems that i have to study how gorilla does it and where in the pipeline the wrapper is been used.
system
(system)
Closed
February 3, 2016, 7:21am
7
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.