-
Notifications
You must be signed in to change notification settings - Fork 40
/
http_logging_transport.go
67 lines (57 loc) · 1.47 KB
/
http_logging_transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package seth
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
)
// LoggingTransport is a custom transport to log requests and responses
type LoggingTransport struct {
Transport http.RoundTripper
}
// RoundTrip implements the RoundTripper interface
func (t *LoggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()
reqDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
L.Warn().Err(err).Msg("Error dumping request")
} else {
fmt.Printf("Request:\n%s\n", string(reqDump))
}
transport := t.Transport
if transport == nil {
transport = http.DefaultTransport
}
resp, err := transport.RoundTrip(req)
if err != nil {
return nil, err
}
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
L.Warn().Err(err).Msg("Error dumping response")
} else {
fmt.Printf("Response:\n%s\n", string(respDump))
}
fmt.Printf("Request took %s\n", time.Since(start))
return resp, nil
}
// NewLoggingTransport creates a new logging transport for GAP or default transport
// controlled by SETH_LOG_LEVEL
func NewLoggingTransport() http.RoundTripper {
if strings.EqualFold(os.Getenv(LogLevelEnvVar), "trace") {
return &LoggingTransport{
// TODO: GAP, add proper certificates
Transport: &http.Transport{
//nolint
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
return &http.Transport{
//nolint
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}