Skip to content

Commit

Permalink
etcd can set log config
Browse files Browse the repository at this point in the history
default log level is info, which will log o log of unused logs
  • Loading branch information
xpunch committed Dec 13, 2019
1 parent 885ba8f commit 11e42aa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.4.0
go.uber.org/zap v1.12.0 // indirect
go.uber.org/zap v1.12.0
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5
google.golang.org/grpc v1.25.1
Expand Down
5 changes: 5 additions & 0 deletions registry/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/util/log"
hash "github.com/mitchellh/hashstructure"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -73,6 +74,10 @@ func configure(e *etcdRegistry, opts ...registry.Option) error {
config.Username = u.Username
config.Password = u.Password
}
cfg, ok := e.options.Context.Value(logConfigKey{}).(*zap.Config)
if ok && cfg != nil {
config.LogConfig = cfg
}
}

var cAddrs []string
Expand Down
13 changes: 13 additions & 0 deletions registry/etcd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"

"github.com/micro/go-micro/registry"
"go.uber.org/zap"
)

type authKey struct{}

type logConfigKey struct{}

type authCreds struct {
Username string
Password string
Expand All @@ -22,3 +25,13 @@ func Auth(username, password string) registry.Option {
o.Context = context.WithValue(o.Context, authKey{}, &authCreds{Username: username, Password: password})
}
}

// LogConfig allows you to set etcd log config
func LogConfig(config *zap.Config) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, authKey{}, config)
}
}

0 comments on commit 11e42aa

Please sign in to comment.