forked from IBM-Cloud/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/google: Log HTTP requests and responses in DEBUG mode (hashi…
- Loading branch information
1 parent
9ec0f53
commit f868a59
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package logging | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
type transport struct { | ||
name string | ||
transport http.RoundTripper | ||
} | ||
|
||
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if IsDebugOrHigher() { | ||
reqData, err := httputil.DumpRequestOut(req, true) | ||
if err == nil { | ||
log.Printf("[DEBUG] "+logReqMsg, t.name, string(reqData)) | ||
} else { | ||
log.Printf("[ERROR] %s API Request error: %#v", t.name, err) | ||
} | ||
} | ||
|
||
resp, err := t.transport.RoundTrip(req) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
if IsDebugOrHigher() { | ||
respData, err := httputil.DumpResponse(resp, true) | ||
if err == nil { | ||
log.Printf("[DEBUG] "+logRespMsg, t.name, string(respData)) | ||
} else { | ||
log.Printf("[ERROR] %s API Response error: %#v", t.name, err) | ||
} | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func NewTransport(name string, t http.RoundTripper) *transport { | ||
return &transport{name, t} | ||
} | ||
|
||
const logReqMsg = `%s API Request Details: | ||
---[ REQUEST ]--------------------------------------- | ||
%s | ||
-----------------------------------------------------` | ||
|
||
const logRespMsg = `%s API Response Details: | ||
---[ RESPONSE ]-------------------------------------- | ||
%s | ||
-----------------------------------------------------` |