diff --git a/main.go b/main.go index 98207003..1b4fbddf 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,17 @@ package main import ( "github.com/SumoLogic/terraform-provider-sumologic/sumologic" "github.com/hashicorp/terraform-plugin-sdk/plugin" + "log" ) var version string // provider version is passed as compile time argument var defaultVersion = "dev" func main() { + // Remove any date and time prefix in log package function output to + // prevent duplicate timestamp and incorrect log level setting + // See: https://developer.hashicorp.com/terraform/plugin/log/writing#duplicate-timestamp-and-incorrect-level-messages + log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) if version == "" { sumologic.ProviderVersion = defaultVersion } else { diff --git a/sumologic/sumologic_client.go b/sumologic/sumologic_client.go index 5e30c607..de0aa935 100644 --- a/sumologic/sumologic_client.go +++ b/sumologic/sumologic_client.go @@ -63,7 +63,7 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces func logRequestAndResponse(req *http.Request, resp *http.Response) { var maskedHeader = req.Header.Clone() maskedHeader.Set("Authorization", "xxxxxxxxxxx") - log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [StatusCode=%s]\n", req.Method, req.URL, maskedHeader, resp.Status) + log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s]\n", req.Method, req.URL, maskedHeader, resp.Status) } func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) { @@ -328,12 +328,17 @@ func (s *Client) Delete(urlPath string) ([]byte, error) { return d, nil } +func ErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) { + log.Printf("[ERROR] Request %s failed after %d attempts with response: [%s]", resp.Request.URL, numTries, resp.Status) + return resp, err +} + func NewClient(accessID, accessKey, authJwt, environment, base_url string, admin bool) (*Client, error) { retryClient := retryablehttp.NewClient() retryClient.RetryMax = 10 // Disable DEBUG logs (https://github.com/hashicorp/go-retryablehttp/issues/31) retryClient.Logger = nil - + retryClient.ErrorHandler = ErrorHandler client := Client{ AccessID: accessID, AccessKey: accessKey,