github.com/posener/terraform@v0.11.0-beta1.0.20171103235147-645df36af025/helper/logging/transport.go (about) 1 package logging 2 3 import ( 4 "log" 5 "net/http" 6 "net/http/httputil" 7 ) 8 9 type transport struct { 10 name string 11 transport http.RoundTripper 12 } 13 14 func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { 15 if IsDebugOrHigher() { 16 reqData, err := httputil.DumpRequestOut(req, true) 17 if err == nil { 18 log.Printf("[DEBUG] "+logReqMsg, t.name, string(reqData)) 19 } else { 20 log.Printf("[ERROR] %s API Request error: %#v", t.name, err) 21 } 22 } 23 24 resp, err := t.transport.RoundTrip(req) 25 if err != nil { 26 return resp, err 27 } 28 29 if IsDebugOrHigher() { 30 respData, err := httputil.DumpResponse(resp, true) 31 if err == nil { 32 log.Printf("[DEBUG] "+logRespMsg, t.name, string(respData)) 33 } else { 34 log.Printf("[ERROR] %s API Response error: %#v", t.name, err) 35 } 36 } 37 38 return resp, nil 39 } 40 41 func NewTransport(name string, t http.RoundTripper) *transport { 42 return &transport{name, t} 43 } 44 45 const logReqMsg = `%s API Request Details: 46 ---[ REQUEST ]--------------------------------------- 47 %s 48 -----------------------------------------------------` 49 50 const logRespMsg = `%s API Response Details: 51 ---[ RESPONSE ]-------------------------------------- 52 %s 53 -----------------------------------------------------`