如何从http.ResponseWriter获取数据进行日志记录 [英] How to get data from http.ResponseWriter for logging

查看:85
本文介绍了如何从http.ResponseWriter获取数据进行日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这个问题是否有意义,但是我想知道是否有任何方法可以获取以 http.ResponseWriter 编写的数据.我需要它来记录日志.

I don't know if this question makes any sense, but I was wondering If there is any way to get the data which is written in http.ResponseWriter. I need it for logging.

我已经用Go编写了一个API.

I have written an API in Go.

func api1(w http.ResponseWriter, req *http.Request) {       
    var requestData MyStruct
    err := json.NewDecoder(req.Body).Decode(&requestData)
    if err != nil {
        writeError(w, "JSON request is not in correct format")
        return
    }
    log.Println(" Request Data :", req.Body) // I am logging req
    result, err := compute()                 // getting result from a function

    if err != nil {
        errRes := ErrorResponse{"ERROR", err}
        response, er = json.Marshal(errRes) // getting error response
    } else {
        response, er = json.Marshal(result)
    }
    if er != nil {
        http.Error(w, er.Error(), 500) // writing error
        return
    }
    io.WriteString(w, string(response)) // writing response
}

目的是创建一个包含请求和响应数据的日志.该响应可以是错误响应或已处理响应.

The aim is to create a single log with request and response data. The response can be either an error response or processed response.

我在想是否可以获取写在http.ResponseWriter上的数据,因此我可以创建一个有意义的日志.

I was thinking if I could get data which is written on http.ResponseWriter then, I can create single meaningful log.

这可能吗?如果没有,请提出我该如何实现.

Is this possible? If not, please suggest how can I achieve this.

推荐答案

您可以使用 io.MultiWriter -它创建一个写入器,将其写入复制到所有提供的写入器中.因此要记录响应

You could use io.MultiWriter - it creates a writer that duplicates its writes to all the provided writers. So to log the reponse

func api1(w http.ResponseWriter, req *http.Request) {
    var log bytes.Buffer
    rsp := io.MultiWriter(w, &log)
    // from this point on use rsp instead of w, ie
    err := json.NewDecoder(req.Body).Decode(&requestData)
    if err != nil {
        writeError(rsp, "JSON request is not in correct format")
        return
    }
    ...
}

现在,您在 w log 中都将写入 rsp 的信息重复了,您可以保存的内容将缓冲区登录到光盘上,以便在控制台等上显示它​​.

Now you have duplicate of the info written into rsp in both w and log and you can save the content of the log buffer onto disc of show it on console etc.

您可以使用 io.TeeReader 创建一个读取器,该读取器向给定的Writer写入内容从给定的阅读器读取-这将使您可以将 req.Body 的副本保存到日志中,即

You can use io.TeeReader to create a Reader that writes to given Writer what it reads from given reader - this would allow you to save copy of the req.Body into log, ie

func api1(w http.ResponseWriter, req *http.Request) {
    var log bytes.Buffer
    tee := io.TeeReader(req.Body, &log)
    err := json.NewDecoder(tee).Decode(&requestData)
    ...
}

现在,由于json解码器读取形式为 tee ,因此 req.Body 的内容也被复制到 log 缓冲区中.

Now since json decoder reads form tee the content of the req.Body is also copied into the log buffer.

这篇关于如何从http.ResponseWriter获取数据进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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