Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: upgrade to grpclog.LoggerV2 #8533

Merged
merged 3 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clientv3/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package clientv3_test
import (
"context"
"log"
"os"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/pkg/capnslog"

"google.golang.org/grpc/grpclog"
)

var (
Expand All @@ -31,8 +33,7 @@ var (
)

func Example() {
var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "clientv3")
clientv3.SetLogger(plog)
clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))

cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
Expand Down
33 changes: 23 additions & 10 deletions clientv3/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,35 @@ package clientv3

import (
"io/ioutil"
"log"
"sync"

"google.golang.org/grpc/grpclog"
)

// Logger is the logger used by client library.
// It implements grpclog.Logger interface.
type Logger grpclog.Logger
// It implements grpclog.LoggerV2 interface.
type Logger grpclog.LoggerV2

var (
logger settableLogger
)

type settableLogger struct {
l grpclog.Logger
l grpclog.LoggerV2
mu sync.RWMutex
}

func init() {
// disable client side logs by default
logger.mu.Lock()
logger.l = log.New(ioutil.Discard, "", 0)
logger.l = grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard)

// logger has to override the grpclog at initialization so that
// any changes to the grpclog go through logger with locking
// instead of through SetLogger
//
// now updates only happen through settableLogger.set
grpclog.SetLogger(&logger)
grpclog.SetLoggerV2(&logger)
logger.mu.Unlock()
}

Expand All @@ -72,11 +71,25 @@ func (s *settableLogger) get() Logger {
return l
}

// implement the grpclog.Logger interface
// implement the grpclog.LoggerV2 interface

func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
func (s *settableLogger) Warningf(format string, args ...interface{}) {
s.get().Warningf(format, args...)
}
func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
func (s *settableLogger) Errorf(format string, args ...interface{}) {
s.get().Errorf(format, args...)
}
func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
func (s *settableLogger) Print(args ...interface{}) { s.get().Print(args...) }
func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Printf(format, args...) }
func (s *settableLogger) Println(args ...interface{}) { s.get().Println(args...) }
func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
func (s *settableLogger) V(l int) bool { return s.get().V(l) }
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"time"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/coreos/etcd/pkg/srv"
"github.com/coreos/etcd/pkg/transport"
"github.com/spf13/cobra"
"google.golang.org/grpc/grpclog"
)

// GlobalFlags are flags that defined globally
Expand Down Expand Up @@ -97,7 +97,7 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
ExitWithError(ExitError, derr)
}
if debug {
clientv3.SetLogger(log.New(os.Stderr, "grpc: ", 0))
clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
}

endpoints, err := endpointsFromCmd(cmd)
Expand Down
3 changes: 2 additions & 1 deletion etcdserver/api/v3rpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v3rpc
import (
"crypto/tls"
"math"
"os"

"github.com/coreos/etcd/etcdserver"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
Expand All @@ -35,7 +36,7 @@ const (
)

func init() {
grpclog.SetLogger(plog)
grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
}

func Server(s *etcdserver.EtcdServer, tls *tls.Config) *grpc.Server {
Expand Down
4 changes: 2 additions & 2 deletions tools/benchmark/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"context"
"crypto/rand"
"fmt"
"log"
"os"
"strings"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/report"
"google.golang.org/grpc/grpclog"
)

var (
Expand Down Expand Up @@ -98,7 +98,7 @@ func mustCreateConn() *clientv3.Client {
return mustCreateConn()
}

clientv3.SetLogger(log.New(os.Stderr, "grpc", 0))
clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))

if err != nil {
fmt.Fprintf(os.Stderr, "dial error: %v\n", err)
Expand Down
3 changes: 2 additions & 1 deletion tools/functional-tester/etcd-tester/stresser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"fmt"
"os"
"strings"
"sync"
"time"
Expand All @@ -24,7 +25,7 @@ import (
"google.golang.org/grpc/grpclog"
)

func init() { grpclog.SetLogger(plog) }
func init() { grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr)) }

type Stresser interface {
// Stress starts to stress the etcd cluster
Expand Down