mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #12954 from serathius/logger-new-ctx-client
client: Add logger argument to NewCtxClient
This commit is contained in:
commit
3cb1ba4b2b
@ -83,11 +83,21 @@ func New(cfg Config) (*Client, error) {
|
|||||||
// NewCtxClient creates a client with a context but no underlying grpc
|
// NewCtxClient creates a client with a context but no underlying grpc
|
||||||
// connection. This is useful for embedded cases that override the
|
// connection. This is useful for embedded cases that override the
|
||||||
// service interface implementations and do not need connection management.
|
// service interface implementations and do not need connection management.
|
||||||
func NewCtxClient(ctx context.Context) *Client {
|
func NewCtxClient(ctx context.Context, opts ...Option) *Client {
|
||||||
cctx, cancel := context.WithCancel(ctx)
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
return &Client{ctx: cctx, cancel: cancel, lgMu: new(sync.RWMutex), lg: zap.NewNop()}
|
c := &Client{ctx: cctx, cancel: cancel, lgMu: new(sync.RWMutex)}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(c)
|
||||||
|
}
|
||||||
|
if c.lg == nil {
|
||||||
|
c.lg = zap.NewNop()
|
||||||
|
}
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Option is a function type that can be passed as argument to NewCtxClient to configure client
|
||||||
|
type Option func(*Client)
|
||||||
|
|
||||||
// NewFromURL creates a new etcdv3 client from a URL.
|
// NewFromURL creates a new etcdv3 client from a URL.
|
||||||
func NewFromURL(url string) (*Client, error) {
|
func NewFromURL(url string) (*Client, error) {
|
||||||
return New(Config{Endpoints: []string{url}})
|
return New(Config{Endpoints: []string{url}})
|
||||||
@ -98,9 +108,16 @@ func NewFromURLs(urls []string) (*Client, error) {
|
|||||||
return New(Config{Endpoints: urls})
|
return New(Config{Endpoints: urls})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithZapLogger is a NewCtxClient option that overrides the logger
|
||||||
|
func WithZapLogger(lg *zap.Logger) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.lg = lg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithLogger overrides the logger.
|
// WithLogger overrides the logger.
|
||||||
//
|
//
|
||||||
// Deprecated: Please use Logger field in clientv3.Config
|
// Deprecated: Please use WithZapLogger or Logger field in clientv3.Config
|
||||||
//
|
//
|
||||||
// Does not changes grpcLogger, that can be explicitly configured
|
// Does not changes grpcLogger, that can be explicitly configured
|
||||||
// using grpc_zap.ReplaceGrpcLoggerV2(..) method.
|
// using grpc_zap.ReplaceGrpcLoggerV2(..) method.
|
||||||
|
@ -17,6 +17,7 @@ package clientv3
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.uber.org/zap"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -187,3 +188,13 @@ func TestWithLogger(t *testing.T) {
|
|||||||
t.Errorf("WithLogger should modify *zap.Logger")
|
t.Errorf("WithLogger should modify *zap.Logger")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestZapWithLogger(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
lg := zap.NewNop()
|
||||||
|
c := NewCtxClient(ctx, WithZapLogger(lg))
|
||||||
|
|
||||||
|
if c.lg != lg {
|
||||||
|
t.Errorf("WithZapLogger should modify *zap.Logger")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -28,11 +28,7 @@ import (
|
|||||||
// of making gRPC calls through sockets, the client makes direct function calls
|
// of making gRPC calls through sockets, the client makes direct function calls
|
||||||
// to the etcd server through its api/v3rpc function interfaces.
|
// to the etcd server through its api/v3rpc function interfaces.
|
||||||
func New(s *etcdserver.EtcdServer) *clientv3.Client {
|
func New(s *etcdserver.EtcdServer) *clientv3.Client {
|
||||||
c := clientv3.NewCtxClient(context.Background())
|
c := clientv3.NewCtxClient(context.Background(), clientv3.WithZapLogger(s.Logger()))
|
||||||
lg := s.Logger()
|
|
||||||
if lg != nil {
|
|
||||||
c.WithLogger(lg)
|
|
||||||
}
|
|
||||||
|
|
||||||
kvc := adapter.KvServerToKvClient(v3rpc.NewQuotaKVServer(s))
|
kvc := adapter.KvServerToKvClient(v3rpc.NewQuotaKVServer(s))
|
||||||
c.KV = clientv3.NewKVFromKVClient(kvc, c)
|
c.KV = clientv3.NewKVFromKVClient(kvc, c)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user