如何在杜松子酒中记录响应身体 [英] How to log response body in gin

查看:156
本文介绍了如何在杜松子酒中记录响应身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将响应主体记录在杜松子酒的中间件中,但我没有找到如何获得响应主体。任何人都可以帮忙吗?



我正在使用这样的中间件:

  func Logger()gin.HandlerFunc {

return func(c * gin.Context){

c.Next()

statusCode: = c.Writer.Status()
if statusCode> = 400 {
//确定这是一个有错误的请求,让我们为它记录
// log body here


$ b $ / code $ / pre

我的问题是,如何得到回应你需要拦截响应的写入并将其存储在第一个地方。然后你可以登录它。为此,您需要实现自己的Writer截取Write()调用。



例如,如下所示:

  type bodyLogWriter struct {
gin.ResponseWriter
body * bytes.Buffer
}

func(w bodyLogWriter)写(b []字节)(int,错误){
w.body.Write(b)
return w.ResponseWriter.Write(b)
}

func ginBodyLogMiddleware(c * gin.Context){
blw:=& bodyLogWriter {body:bytes.NewBufferString(),ResponseWriter:c.Writer}
c.Writer = blw
c.Next()
statusCode:= c.Writer.Status()
if statusCode> = 400 {
//确定这是一个有错误的请求,让我们为它
//现在打印body(或者以你喜欢的方式登录)
fmt.Println(Response body:+ blw.body.String())
}
}

然后使用这样的中间件:

  router.Use(ginBodyLogMiddleware) 

请注意,这个窗台不适用于静态文件,因为杜松子酒似乎没有使用c.Writer为他们。但在大多数情况下,这就是你想要的。

如果您想截取所有文件,您需要使用稍微复杂的方法。而不是中间件,你需要实现一个封装器http.Handler来包装gin.Engine,并且将使用与上面所示相同的方法截取并记录写入http.ResponseWriter的任何内容。然后运行这样的杜松子酒服务器:

  ginRouter:= gin.New()
//配置您的中间件和路由根据需要

//运行http服务器,如下所示,其中bodyLogHandler是您的包装处理程序
http.ListenAndServe(bindAddress,& bodyLogHandler {wrappedHandler:ginRouter}


I need to log the response body in a middleware of gin, but I don't find how to get the response body. Can anyone help?

I am using a middleware like this:

func Logger() gin.HandlerFunc {

    return func(c *gin.Context) {

        c.Next()

        statusCode := c.Writer.Status()
        if statusCode >= 400 {
            //ok this is an request with error, let's make a record for it
            //log body here
        }
    }
}

My question is, how to get response body from Context in middleware?

解决方案

You need to intercept writing of response and store it somewhere first. Then you can log it. And to do that you need to implement your own Writer intercepting Write() calls.

For example, as follows:

type bodyLogWriter struct {
    gin.ResponseWriter
    body *bytes.Buffer
}

func (w bodyLogWriter) Write(b []byte) (int, error) {
    w.body.Write(b)
    return w.ResponseWriter.Write(b)
}

func ginBodyLogMiddleware(c *gin.Context) {
    blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
    c.Writer = blw
    c.Next()
    statusCode := c.Writer.Status()
    if statusCode >= 400 {
        //ok this is an request with error, let's make a record for it
        // now print body (or log in your preferred way)
        fmt.Println("Response body: " + blw.body.String())
    }
}

Then use this middleware like this:

router.Use(ginBodyLogMiddleware)

Note that this sill won't work for static files as gin does not seem to use c.Writer for them. But in most cases, that's what you want anyway.

If you want to intercept all files, you need to use a slightly more complicated approach. Instead of Middleware, you'll need to implement a wrapper http.Handler that will wrap gin.Engine and will use same approach as shown above to intercept and log whatever is written to http.ResponseWriter. Then run gin server like this:

ginRouter := gin.New()
// configure your middleware and routes as required

// Run http server as follows, where bodyLogHandler is your wrapper handler
http.ListenAndServe(bindAddress, &bodyLogHandler{wrappedHandler: ginRouter}

这篇关于如何在杜松子酒中记录响应身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆