Giving control to the user of the library as to how they want to use a logger. Logger for gorealis only needs Print, Printf, and Println receiver functions. Example in the client uses the std library log.

This commit is contained in:
Renan DelValle 2017-11-29 12:27:07 -08:00
parent 878a11f896
commit d074fd9d0c
3 changed files with 17 additions and 23 deletions

View file

@ -27,6 +27,7 @@ import (
"github.com/paypal/gorealis" "github.com/paypal/gorealis"
"github.com/paypal/gorealis/gen-go/apache/aurora" "github.com/paypal/gorealis/gen-go/apache/aurora"
"github.com/paypal/gorealis/response" "github.com/paypal/gorealis/response"
"log"
) )
var cmd, executor, url, clustersConfig, clusterName, updateId, username, password, zkUrl, hostList string var cmd, executor, url, clustersConfig, clusterName, updateId, username, password, zkUrl, hostList string
@ -87,6 +88,7 @@ func main() {
Factor: 2.0, Factor: 2.0,
Jitter: 0.1, Jitter: 0.1,
}), }),
realis.SetLogger(log.New(os.Stdout, "realis-debug: ", log.Ldate)),
} }
//check if zkUrl is available. //check if zkUrl is available.

View file

@ -25,11 +25,6 @@ import (
"math/rand" "math/rand"
"log"
"io/ioutil"
"os"
"git.apache.org/thrift.git/lib/go/thrift" "git.apache.org/thrift.git/lib/go/thrift"
"github.com/paypal/gorealis/gen-go/apache/aurora" "github.com/paypal/gorealis/gen-go/apache/aurora"
"github.com/paypal/gorealis/response" "github.com/paypal/gorealis/response"
@ -74,7 +69,7 @@ type realisClient struct {
client *aurora.AuroraSchedulerManagerClient client *aurora.AuroraSchedulerManagerClient
readonlyClient *aurora.ReadOnlySchedulerClient readonlyClient *aurora.ReadOnlySchedulerClient
adminClient *aurora.AuroraAdminClient adminClient *aurora.AuroraAdminClient
logger *log.Logger logger Logger
} }
type RealisConfig struct { type RealisConfig struct {
@ -86,6 +81,7 @@ type RealisConfig struct {
backoff *Backoff backoff *Backoff
transport thrift.TTransport transport thrift.TTransport
protoFactory thrift.TProtocolFactory protoFactory thrift.TProtocolFactory
logger Logger
} }
type Backoff struct { type Backoff struct {
@ -160,6 +156,13 @@ func BackOff(b *Backoff) ClientOption {
} }
} }
// Using the word set to avoid name collision with Interface
func SetLogger(l Logger) ClientOption {
return func(config *RealisConfig) {
config.logger = l
}
}
func newTJSONTransport(url string, timeout int) (thrift.TTransport, error) { func newTJSONTransport(url string, timeout int) (thrift.TTransport, error) {
trans, err := defaultTTransport(url, timeout) trans, err := defaultTTransport(url, timeout)
if err != nil { if err != nil {
@ -191,21 +194,14 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
// Default configs // Default configs
config.timeoutms = 10000 config.timeoutms = 10000
config.backoff = &defaultBackoff config.backoff = &defaultBackoff
config.logger = NoopLogger{}
// Override default configs where necessary // Override default configs where necessary
for _, opt := range options { for _, opt := range options {
opt(config) opt(config)
} }
// Use a no-op logger if we didn't compile in debug mode. config.logger.Println("Number of options applied to config: ", len(options))
var logger *log.Logger
if realisDebug {
logger = log.New(os.Stdout, "gorealis: ", log.Ltime|log.Ldate)
} else {
logger = log.New(ioutil.Discard, "", 0)
}
logger.Println("Number of options applied to config: ", len(options))
//Set default Transport to JSON if needed. //Set default Transport to JSON if needed.
if !config.jsonTransport && !config.binTransport { if !config.jsonTransport && !config.binTransport {
@ -223,10 +219,10 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "LeaderFromZK error") return nil, errors.Wrap(err, "LeaderFromZK error")
} }
logger.Println("Scheduler URL from ZK: ", url) config.logger.Println("Scheduler URL from ZK: ", url)
} else if config.url != "" { } else if config.url != "" {
url = config.url url = config.url
logger.Println("Scheduler URL: ", url) config.logger.Println("Scheduler URL: ", url)
} else { } else {
return nil, errors.New("Incomplete Options -- url or cluster required") return nil, errors.New("Incomplete Options -- url or cluster required")
} }
@ -248,7 +244,7 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault() config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
} }
logger.Printf("gorealis config url: %+v\n", config.url) config.logger.Printf("gorealis config url: %+v\n", config.url)
//Basic Authentication. //Basic Authentication.
if config.username != "" && config.password != "" { if config.username != "" && config.password != "" {
@ -260,7 +256,7 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory), client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory),
readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory), readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory),
adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory), adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory),
logger: logger}, nil logger: config.logger}, nil
} }

4
zk.go
View file

@ -36,10 +36,6 @@ type ServiceInstance struct {
Status string `json:"status"` Status string `json:"status"`
} }
type NoopLogger struct{}
func (NoopLogger) Printf(format string, a ...interface{}) {
}
// Retrieves current Aurora leader from ZK. // Retrieves current Aurora leader from ZK.
func LeaderFromZK(cluster Cluster) (string, error) { func LeaderFromZK(cluster Cluster) (string, error) {