Home >Backend Development >Golang >How to implement logging of HTTP requests using Go and http.Transport?
How to implement logging of HTTP requests using Go and http.Transport?
When using Go language to make HTTP requests, we often encounter situations where we need to record the detailed information of the request, such as recording the requested URL, request method, request header, request body, etc. This information is very helpful for debugging and troubleshooting issues. This article will introduce how to implement logging of HTTP requests using Go and http.Transport.
In Go language, we can use the http package to make HTTP requests, and http.Transport is responsible for sending and receiving HTTP requests and responses. By customizing the RoundTrip method of http.Transport, we can record the request log before sending the request and after receiving the response.
The following is a sample code:
package main import ( "log" "net/http" "net/http/httputil" "os" "time" ) // LoggingTransport 实现了http.RoundTripper接口 type LoggingTransport struct { Transport http.RoundTripper Logger *log.Logger } // RoundTrip 实现了http.RoundTripper接口的RoundTrip方法 func (t *LoggingTransport) RoundTrip(req *http.Request) (*http.Response, error) { startTime := time.Now() // 打印请求信息 dump, err := httputil.DumpRequestOut(req, true) if err != nil { return nil, err } t.Logger.Println(string(dump)) // 发送请求 resp, err := t.Transport.RoundTrip(req) if err != nil { return nil, err } // 打印响应信息 dump, err = httputil.DumpResponse(resp, true) if err != nil { return nil, err } t.Logger.Println(string(dump)) // 计算请求耗时并打印 duration := time.Since(startTime) t.Logger.Printf("Request took %s", duration) return resp, nil } func main() { // 创建自定义Transport transport := &LoggingTransport{ Transport: http.DefaultTransport, Logger: log.New(os.Stdout, "", log.LstdFlags), } // 创建自定义的http.Client client := &http.Client{ Transport: transport, } // 创建自定义的http.Request req, err := http.NewRequest("GET", "http://www.example.com", nil) if err != nil { log.Fatal(err) } // 发送请求 resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() // 处理响应 // ... }
In the above code, we first define a LoggingTransport type, which implements the RoundTrip method of the http.RoundTripper interface. In this method, we first get the current time as the request start time, then use the DumpRequestOut method of the httputil package to convert the request information into a byte array and write it to the log file, then send the request, and then use the DumpResponse method to convert the response information into The byte array is written to the log file, and finally the request time is calculated and printed.
In the main function, we create a custom Transport and Client, pass them to the Transport field of http.Client and the last parameter of the http.NewRequest function respectively, and then send the request and process the response.
In this way, we can easily implement logging of HTTP requests. Log information can be output to the console, written to a file, or sent to the log collection system according to actual needs.
Summary: This article introduces how to use Go and http.Transport to implement logging of HTTP requests. We customize the RoundTrip method of http.Transport to record the details of the request before sending the request and after receiving the response, so as to facilitate debugging and troubleshooting. Hope this article is helpful to you!
The above is the detailed content of How to implement logging of HTTP requests using Go and http.Transport?. For more information, please follow other related articles on the PHP Chinese website!