diff --git a/pkg/client/options.go b/pkg/client/options.go index f9c0ffcf5..4dbfb6626 100644 --- a/pkg/client/options.go +++ b/pkg/client/options.go @@ -15,7 +15,9 @@ package client import ( + "log" "net/http" + "os" "github.com/hashicorp/go-retryablehttp" ) @@ -34,10 +36,17 @@ const ( DefaultRetryCount = 3 ) +var DefaultLogger retryablehttp.Logger + +func init() { + DefaultLogger = log.New(os.Stderr, "", log.LstdFlags) +} + func makeOptions(opts ...Option) *options { o := &options{ UserAgent: "", RetryCount: DefaultRetryCount, + Logger: DefaultLogger, } for _, opt := range opts { diff --git a/pkg/client/options_test.go b/pkg/client/options_test.go index 65ca7dfb5..8e763b50e 100644 --- a/pkg/client/options_test.go +++ b/pkg/client/options_test.go @@ -15,13 +15,17 @@ package client import ( + "log" "net/http" + "os" "testing" "github.com/google/go-cmp/cmp" ) func TestMakeOptions(t *testing.T) { + customLogger := log.New(os.Stdout, "", log.LstdFlags) + tests := []struct { desc string @@ -29,20 +33,24 @@ func TestMakeOptions(t *testing.T) { want *options }{{ desc: "no opts", - want: &options{RetryCount: DefaultRetryCount}, + want: &options{RetryCount: DefaultRetryCount, Logger: DefaultLogger}, }, { desc: "WithUserAgent", opts: []Option{WithUserAgent("test user agent")}, - want: &options{UserAgent: "test user agent", RetryCount: DefaultRetryCount}, + want: &options{UserAgent: "test user agent", RetryCount: DefaultRetryCount, Logger: DefaultLogger}, }, { desc: "WithRetryCount", opts: []Option{WithRetryCount(2)}, - want: &options{UserAgent: "", RetryCount: 2}, + want: &options{UserAgent: "", RetryCount: 2, Logger: DefaultLogger}, + }, { + desc: "WithLogger", + opts: []Option{WithLogger(customLogger)}, + want: &options{UserAgent: "", RetryCount: DefaultRetryCount, Logger: customLogger}, }} for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { got := makeOptions(tc.opts...) - if d := cmp.Diff(tc.want, got); d != "" { + if d := cmp.Diff(tc.want, got, cmp.Comparer(func(a, b *log.Logger) bool { return a == b })); d != "" { t.Errorf("makeOptions(%v) returned unexpected result (-want +got): %s", tc.desc, d) } }) diff --git a/pkg/client/rekor_client.go b/pkg/client/rekor_client.go index 58f44b400..11b24b43c 100644 --- a/pkg/client/rekor_client.go +++ b/pkg/client/rekor_client.go @@ -35,9 +35,7 @@ func GetRekorClient(rekorServerURL string, opts ...Option) (*client.Rekor, error retryableClient := retryablehttp.NewClient() retryableClient.RetryMax = int(o.RetryCount) - if o.Logger != nil { - retryableClient.Logger = o.Logger - } + retryableClient.Logger = o.Logger rt := httptransport.NewWithClient(url.Host, client.DefaultBasePath, []string{url.Scheme}, retryableClient.StandardClient()) rt.Consumers["application/json"] = runtime.JSONConsumer()