clientv3: configurable grpc logger

release-3.0
Gyu-Ho Lee 2016-03-26 22:05:22 -07:00
parent b8fc61bcec
commit 29fccb3221
1 changed files with 18 additions and 0 deletions

View File

@ -17,6 +17,8 @@ package clientv3
import ( import (
"crypto/tls" "crypto/tls"
"errors" "errors"
"io/ioutil"
"log"
"net" "net"
"net/url" "net/url"
"strings" "strings"
@ -26,12 +28,15 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
) )
var ( var (
ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints") ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
) )
type Logger grpclog.Logger
// Client provides and manages an etcd v3 client session. // Client provides and manages an etcd v3 client session.
type Client struct { type Client struct {
Cluster Cluster
@ -49,6 +54,8 @@ type Client struct {
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
logger Logger
} }
// EndpointDialer is a policy for choosing which endpoint to dial next // EndpointDialer is a policy for choosing which endpoint to dial next
@ -66,6 +73,9 @@ type Config struct {
// TLS holds the client secure credentials, if any. // TLS holds the client secure credentials, if any.
TLS *tls.Config TLS *tls.Config
// Logger is the logger used by client library.
Logger Logger
} }
// New creates a new etcdv3 client from a given configuration. // New creates a new etcdv3 client from a given configuration.
@ -180,6 +190,14 @@ func newClient(cfg *Config) (*Client, error) {
client.Watcher = NewWatcher(client) client.Watcher = NewWatcher(client)
client.Auth = NewAuth(client) client.Auth = NewAuth(client)
client.Maintenance = &maintenance{c: client} client.Maintenance = &maintenance{c: client}
if cfg.Logger == nil {
client.logger = log.New(ioutil.Discard, "", 0)
// disable client side grpc by default
grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
} else {
client.logger = cfg.Logger
grpclog.SetLogger(cfg.Logger)
}
return client, nil return client, nil
} }