Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
Add logging to mcset
Browse files Browse the repository at this point in the history
Set a logger on the object, using logging package as default.
Follows the pattern found in the go-zookeeper lib.
  • Loading branch information
paulmach committed Nov 9, 2015
1 parent e2c89e7 commit 1a44999
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions mcset/mcset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mcset

import (
"errors"
"log"
"net"
"sort"
"sync"
Expand All @@ -14,8 +15,16 @@ import (
var (
// ErrNoServers is returned when no servers are configured or available.
ErrNoServers = errors.New("mcset: no servers configured or available")

// DefaultLogger is used by default to print change event messages.
DefaultLogger Logger = defaultLogger{}
)

// Logger is an interface that can be implemented to provide custom log output.
type Logger interface {
Printf(string, ...interface{})
}

// A Watcher represents how a serverset.Watch is used so it can be stubbed out for tests.
type Watcher interface {
Endpoints() []string
Expand All @@ -31,6 +40,8 @@ type MCSet struct {
LastEvent time.Time
EventCount int

Logger Logger

// This channel will get an event when zookeeper updates things
// calling SetEndpoints will not trigger this type of event.
event chan struct{}
Expand All @@ -48,6 +59,7 @@ type MCSet struct {
func New(watch Watcher) *MCSet {
mcset := &MCSet{
Watcher: watch,
Logger: DefaultLogger,
event: make(chan struct{}, 1),
}

Expand Down Expand Up @@ -105,6 +117,8 @@ func (s *MCSet) setEndpoints(endpoints []string) {

sort.StringSlice(endpoints).Sort()

s.Logger.Printf("new endpoints for mcset: %v", endpoints)

s.consistent = consistenthash.New(150, mmh3.Sum32)
s.consistent.Add(endpoints...)
}
Expand Down Expand Up @@ -160,3 +174,9 @@ func (s *MCSet) triggerEvent() {
default:
}
}

type defaultLogger struct{}

func (defaultLogger) Printf(format string, a ...interface{}) {
log.Printf(format, a...)
}

0 comments on commit 1a44999

Please sign in to comment.