You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I wanted to create a simple logger to mimic apache access logs, it is currently impossible to get the written status code. http.ResponseWriter's implementation is http.response which is unexported. As such there is no way to get to it's also unexported http.response.status.
You can get the entire header map from http.ResponseWriter.Header() however it won't give you anything about the status because it never actually gets written to that map.
Mimicking apache access logs may not be ideal, but I will surely want to record the return code in some way.
Some (broken) sample code trying to implement a proper access_log logger.
func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr)
if err != nil {
c.Abort(500)
}
// before request
c.Next()
// after request
var user string
if c.Req.URL.User != nil {
user = c.Req.URL.User.Username()
} else {
user = "-"
}
latency := time.Since(t)
statusCode := c.Writer.(*http.Response).StatusCode
//statusCode := c.Writer.(*http.response).status
log.Print(latency)
fmt.Printf("%v - %v [%v] \"%v %v %v\" %v %v\n",
ip.IP, user, t, c.Req.Method, c.Req.URL.Path,
c.Req.Proto, statusCode, c.Req.ContentLength)
}
}
The text was updated successfully, but these errors were encountered:
If I wanted to create a simple logger to mimic apache access logs, it is currently impossible to get the written status code.
http.ResponseWriter
's implementation ishttp.response
which is unexported. As such there is no way to get to it's also unexportedhttp.response.status
.You can get the entire header map from
http.ResponseWriter.Header()
however it won't give you anything about the status because it never actually gets written to that map.Mimicking apache access logs may not be ideal, but I will surely want to record the return code in some way.
Some (broken) sample code trying to implement a proper access_log logger.
The text was updated successfully, but these errors were encountered: