Skip to content

Commit

Permalink
Provide clean method for overriding a logger and integrating with oth…
Browse files Browse the repository at this point in the history
…er logging frameworks
  • Loading branch information
radekg committed Jan 12, 2022
1 parent 0f3714b commit 5e441a9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
13 changes: 11 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type YBClient interface {
// Execute executes the payload against the service
// and populates the response with the response data.
Execute(payload, response protoreflect.ProtoMessage) error
// Allows configuring the logger used by the client.
// Uses go-hclog. Users can provide integrate with any logging
// framework using https://pkg.go.dev/github.com/hashicorp/go-hclog#InterceptLogger.
WithLogger(logger hclog.Logger) YBClient
}

var (
Expand All @@ -46,14 +50,19 @@ type defaultYBClient struct {
}

// NewYBClient constructs a new instance of the high-level YugabyteDB client.
func NewYBClient(config *configs.YBClientConfig, logger hclog.Logger) YBClient {
func NewYBClient(config *configs.YBClientConfig) YBClient {
return &defaultYBClient{
config: config.WithDefaults(),
lock: &sync.Mutex{},
logger: logger,
logger: hclog.Default(),
}
}

func (c *defaultYBClient) WithLogger(logger hclog.Logger) YBClient {
c.logger = logger
return c
}

func (c *defaultYBClient) Close() error {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down
5 changes: 2 additions & 3 deletions testutils/master/master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/hashicorp/go-hclog"
"github.com/radekg/yugabyte-db-go-client/client"
"github.com/radekg/yugabyte-db-go-client/configs"
"github.com/radekg/yugabyte-db-go-client/errors"
Expand All @@ -25,7 +24,7 @@ func TestMasterIntegration(t *testing.T) {
client := client.NewYBClient(&configs.YBClientConfig{
MasterHostPort: testCtx.MasterExternalAddresses(),
OpTimeout: time.Duration(time.Second * 5),
}, hclog.Default())
})

common.Eventually(t, 15, func() error {
if err := client.Connect(); err != nil {
Expand Down Expand Up @@ -64,7 +63,7 @@ func TestMasterReconnect(t *testing.T) {
OpTimeout: time.Duration(time.Second * 5),
MaxReconnectAttempts: 1,
ReconnectRetryInterval: time.Duration(time.Millisecond * 100),
}, hclog.Default())
})

errNotConnected := client.Execute(request, &ybApi.ListMastersResponsePB{})
assert.NotNil(t, errNotConnected, "expected an error")
Expand Down
5 changes: 2 additions & 3 deletions testutils/tserver/tserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

"github.com/hashicorp/go-hclog"
"github.com/radekg/yugabyte-db-go-client/client"
"github.com/radekg/yugabyte-db-go-client/configs"
"github.com/radekg/yugabyte-db-go-client/testutils/common"
Expand All @@ -30,7 +29,7 @@ func TestTServerIntegration(t *testing.T) {
client := client.NewYBClient(&configs.YBClientConfig{
MasterHostPort: masterTestCtx.MasterExternalAddresses(),
OpTimeout: time.Duration(time.Second * 5),
}, hclog.Default())
})

common.Eventually(t, 15, func() error {
if err := client.Connect(); err != nil {
Expand All @@ -48,7 +47,7 @@ func TestTServerIntegration(t *testing.T) {
if err != nil {
return err
}
t.Log(" ==> Received master list", response)
t.Log("Received master list", response)
return nil
})

Expand Down

0 comments on commit 5e441a9

Please sign in to comment.