go - 如何从中间件记录响应正文?
问题描述
我试图从中间件记录响应正文。我可以获得与 Request 对象相关的所有数据。现在尝试记录单个请求的响应代码和响应正文。
我试图从 中捕获响应正文和响应代码w http.ResponseWriter
,但未能成功。这是示例代码。我正在使用 go 的默认值http
和logrus
日志记录。
next.ServeHTTP(w, r.WithContext(ctx))
end := time.Now().UTC()
latency := end.Sub(start)
// request logging
log.WithFields(log.Fields{
"path": r.URL.Path,
"method": r.Method,
"body": body,
"auth_user": claims,
"latency": latency,
"useragent": r.Header.Get("User-Agent"),
"response_body": ,// cannot get response body here
"status": "", //cannot get response header here
}).Info("request data")
有人可以帮我吗?
解决方案
推荐阅读
- javascript - 如何将由 for 循环创建的元素附加到现有 div?
- javascript - Cookie 未使用 Content-type: application/json 发送
- java - Retrofit调用和Jsoup连接有什么区别?他们怎么可能返回具有相同 URL 的不同响应?
- javascript - 引导反应./node_modules/bootstrap/dist/js/bootstrap.min.js 找不到模块:无法解析'jquery'
- python - 基于多列过滤 Pandas Dataframe,每列有多个值
- python - 运行“python manage.py makemigrations”命令给我语法错误
- python - 如何在 Django 中使用 form.cleaned_data.get() 将数据从视图传递到成功模板?
- python - 如何更改表格中特定行和列的颜色?
- javascript - 使用逻辑运算符和另一个数组过滤新数组
- phpmailer - phpmailer SMTP 错误“未接受数据”和“检查是否连接时捕获 EOF”