Skip to content

Commit

Permalink
Enable resty debug logging based on TF_LOG
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhung committed Apr 4, 2024
1 parent 702b5c5 commit ca3bc0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.22.4 (Apr 4, 2024)

IMPROVEMENTS:

* Enable Resty's debug logging when `TF_LOG` is set to `DEBUG` or `TRACE`.

Issue [#16](https://github.com/jfrog/terraform-provider-shared/issues/16)
PR: [#58](https://github.com/jfrog/terraform-provider-shared/pull/57)

## 1.22.3 (Apr 4, 2024)

BUG FIXES:
Expand Down
24 changes: 11 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package client

import (
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"slices"
"strings"

"github.com/go-resty/resty/v2"
Expand All @@ -21,19 +22,16 @@ func Build(URL, productId string) (*resty.Client, error) {

restyBase := resty.New().
SetBaseURL(baseUrl).
OnAfterResponse(func(client *resty.Client, response *resty.Response) error {
if response == nil {
return fmt.Errorf("no response found")
OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
tfLogLevel := strings.ToLower(os.Getenv("TF_LOG"))
if slices.Contains([]string{"debug", "trace"}, tfLogLevel) {
r.SetDebug(true)
}

// Don't log the response if we have 413 erorr from call home request
// This happens when we make request to call home endpoint too frequently
// for Artifactory to aggregate. Generally only happens during test execution
if strings.Contains(response.Request.URL, "artifactory/api/system/usage") &&
response.StatusCode() == http.StatusRequestEntityTooLarge {
return nil
}

return nil
}).
OnRequestLog(func(r *resty.RequestLog) error {
// Never log auth token
r.Header.Set("Authorization", "<REDACTED>")
return nil
}).
SetHeader("content-type", "application/json").
Expand Down

0 comments on commit ca3bc0f

Please sign in to comment.