Golang记录http响应(除了请求) [英] Golang logging http responses (in addition to requests)

查看:335
本文介绍了Golang记录http响应(除了请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Go和Gorilla web工具包的mux和handler程序包来构建一个复杂的应用程序,其中一部分需要http服务器。 Gorilla的多路复用器和处理程序包非常好用,我能够成功地启动和运行http服务器,并且记录请求非常简单。

但是,我无法确定如何记录回复。理想情况下,我想要一个类似于Gorilla的LoggingHandler的机制,它可以轻松包装日志机制。



是否有Golang包可以轻松打包/记录响应?有没有办法使用Go或Gorilla的功能,我没有考虑过?

我尝试了一些建议,并着手于使用简约包装的相当简单的解决方案。以下是适合我的解决方案(可随意提供评论,或更好的解决方案):

  import(
fmt
log
net / http
net / http / httptest
net / http / httputil
github .com / gorilla / mux



func logHandler(fn http.HandlerFunc)http.HandlerFunc {
return func(w http.ResponseWriter,r * http.Request){
x,err:= httputil.DumpRequest(r,true)
if err!= nil {
http.Error(w,fmt.Sprint(err),http .StatusInternalServerError)
return

log.Println(fmt.Sprintf(%q,x))
rec:= httptest.NewRecorder()
fn (rec,r)
log.Println(fmt.Sprintf(%q,rec.Body))
}
}

func MessageHandler(w http .ResponseWriter,r * http.Request){
fmt.Fprintln(w,A message was received)
}

和fol低代码将使用上述处理程序:

 
router:= mux.NewRouter()
router .HandleFunc(/,logHandler(MessageHandler))

代码将沿着以下方向行事:

 
2016/07/20 14:44:29GET ... HTTP / 1.1 \r\\\
Host:localhost:8088\r\\\
Acept:* / * \r\\\
User-Agent:curl / 7.43.0\r\\\
\r\\ \\ n
2016/07/20 14:44:29 ... [回复正文]


I am using Go and the Gorilla web toolkit's mux and handler packages to build a complex application, part of which requires a http server. Gorilla's mux and handler packages work wonderfully and I am able to successfully get the http server up and running and it has been quite simple to log requests.

However, I am unable to determine how I may log responses. Ideally, I would like a mechanism, similar to Gorilla's LoggingHandler, that "wraps" the logging mechanism easily.

Is there a Golang package that does easily wraps / logs responses? Is there a way to use Go or Gorilla's capabilities in this fashion that I have not considered?

解决方案

Thanks for the great suggestions. I tried a few of the suggestions and landed on a rather simple solution that uses a minimalist wrapper. Here is the solution that worked for me (feel free to offer comments, or better yet, other solutions):

import (
    "fmt"
    "log"
    "net/http"
    "net/http/httptest"
    "net/http/httputil"
    "github.com/gorilla/mux"
)
:

func logHandler(fn http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        x, err := httputil.DumpRequest(r, true)
        if err != nil {
            http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
            return
        }
        log.Println(fmt.Sprintf("%q", x))
        rec := httptest.NewRecorder()
        fn(rec, r)
        log.Println(fmt.Sprintf("%q", rec.Body))            
    }
}

func MessageHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "A message was received")
}

And the following code will use the aforementioned handler:

:
router := mux.NewRouter()
router.HandleFunc("/", logHandler(MessageHandler))
:

Output from the above code will be something along the lines of:

:
2016/07/20 14:44:29 "GET ... HTTP/1.1\r\nHost: localhost:8088\r\nAccept: */*\r\nUser-Agent: curl/7.43.0\r\n\r\n"
2016/07/20 14:44:29 ...[response body]
:

这篇关于Golang记录http响应(除了请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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