Skip to content

Commit

Permalink
Adds ContextLogger in a backwards compatible way.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Morhovich committed Jun 14, 2021
1 parent f416164 commit 5bcb7e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
32 changes: 25 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,27 @@ var isReleasedAttrVal = expression.Value("1")

// Logger defines the minimum desired logger interface for the lock client.
type Logger interface {
Println(v ...interface{})
}

// ContextLogger defines a logger interface that can be used to pass extra information to the implementation.
// For example, if you use zap, you may have extra fields you want to add to the log line. You
// can add those extra fields to the parent context of calls like AcquireLockWithContext, and then retrieve them in
// your implementation of ContextLogger.
type ContextLogger interface {
Println(ctx context.Context, v ...interface{})
}

type DefaultLogger struct {
Logger *log.Logger
type contextLoggerAdapter struct {
logger Logger
}

func newContextLogAdapter(l Logger) *contextLoggerAdapter {
return &contextLoggerAdapter{logger: l}
}

func (l *DefaultLogger) Println(_ context.Context, v ...interface{}) {
l.Logger.Println(v...)
func (cla *contextLoggerAdapter) Println(_ context.Context, v ...interface{}) {
cla.logger.Println(v)
}

// Client is a dynamoDB based distributed lock client.
Expand All @@ -79,7 +91,7 @@ type Client struct {
locks sync.Map
sessionMonitorCancellations sync.Map

logger Logger
logger ContextLogger

stopHeartbeat context.CancelFunc

Expand All @@ -103,8 +115,8 @@ func New(dynamoDB dynamodbiface.DynamoDBAPI, tableName string, opts ...ClientOpt
leaseDuration: defaultLeaseDuration,
heartbeatPeriod: defaultHeartbeatPeriod,
ownerName: randString(32),
logger: &DefaultLogger{
Logger: log.New(ioutil.Discard, "", 0),
logger: &contextLoggerAdapter{
logger: log.New(ioutil.Discard, "", 0),
},
stopHeartbeat: func() {},
}
Expand Down Expand Up @@ -162,6 +174,12 @@ func DisableHeartbeat() ClientOption {
// WithLogger injects a logger into the client, so its internals can be
// recorded.
func WithLogger(l Logger) ClientOption {
return func(c *Client) { c.logger = &contextLoggerAdapter{l} }
}

// WithContextLogger injects a logger into the client, so its internals can be
// recorded.
func WithContextLogger(l ContextLogger) ClientOption {
return func(c *Client) { c.logger = l }
}

Expand Down
10 changes: 3 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,7 @@ func TestCustomRefreshPeriod(t *testing.T) {
Region: aws.String("us-west-2"),
})
var buf bytes.Buffer
logger := &dynamolock.DefaultLogger{
Logger: log.New(&buf, "", 0),
}
logger := log.New(&buf, "", 0)
c, err := dynamolock.New(svc,
"locks",
dynamolock.WithLeaseDuration(3*time.Second),
Expand Down Expand Up @@ -869,7 +867,7 @@ type testLogger struct {
t *testing.T
}

func (t *testLogger) Println(_ context.Context, v ...interface{}) {
func (t *testLogger) Println(v ...interface{}) {
t.t.Helper()
t.t.Log(v...)
}
Expand Down Expand Up @@ -947,9 +945,7 @@ func TestHeartbeatError(t *testing.T) {
defer func() {
t.Log(buf.String())
}()
logger := &dynamolock.DefaultLogger{
Logger: log.New(&buf, "", 0),
}
logger := log.New(&buf, "", 0)

heartbeatPeriod := 2 * time.Second
c, err := dynamolock.New(svc,
Expand Down

0 comments on commit 5bcb7e6

Please sign in to comment.