Skip to content

Commit 0afb407

Browse files
committed
fix(drand): provide a named logger
1 parent 27987b6 commit 0afb407

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

chain/beacon/drand/drand.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,28 @@ type DrandHTTPClient interface {
7070

7171
type logger struct {
7272
*zap.SugaredLogger
73+
name string
7374
}
7475

7576
func (l *logger) With(args ...interface{}) dlog.Logger {
76-
return &logger{l.SugaredLogger.With(args...)}
77+
return &logger{l.SugaredLogger.With(args...), l.name}
7778
}
7879

7980
func (l *logger) Named(s string) dlog.Logger {
80-
return &logger{l.SugaredLogger.Named(s)}
81+
newName := l.name
82+
if newName != "" {
83+
newName += "."
84+
}
85+
newName += s
86+
return &logger{l.SugaredLogger.Named(s), newName}
8187
}
8288

8389
func (l *logger) AddCallerSkip(skip int) dlog.Logger {
84-
return &logger{l.SugaredLogger.With(zap.AddCallerSkip(skip))}
90+
return &logger{l.SugaredLogger.With(zap.AddCallerSkip(skip)), l.name}
91+
}
92+
93+
func (l *logger) Name() string {
94+
return l.name
8595
}
8696

8797
func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes.DrandConfig) (*DrandBeacon, error) {
@@ -96,7 +106,7 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
96106

97107
var clients []drand.Client
98108
for _, url := range config.Servers {
99-
hc, err := hclient.NewWithInfo(&logger{&log.SugaredLogger}, url, drandChain, nil)
109+
hc, err := hclient.NewWithInfo(&logger{&log.SugaredLogger, "drand"}, url, drandChain, nil)
100110
if err != nil {
101111
return nil, xerrors.Errorf("could not create http drand client: %w", err)
102112
}
@@ -107,7 +117,7 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
107117
opts := []dclient.Option{
108118
dclient.WithChainInfo(drandChain),
109119
dclient.WithCacheSize(1024),
110-
dclient.WithLogger(&logger{&log.SugaredLogger}),
120+
dclient.WithLogger(&logger{&log.SugaredLogger, "drand"}),
111121
}
112122

113123
if ps != nil {

chain/beacon/drand/drand_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestPrintGroupInfo(t *testing.T) {
2222

2323
drandChain, err := dchain.InfoFromJSON(bytes.NewReader([]byte(chainInfo)))
2424
assert.NoError(t, err)
25-
c, err := hclient.NewWithInfo(&logger{&log.SugaredLogger}, server, drandChain, nil)
25+
c, err := hclient.NewWithInfo(&logger{&log.SugaredLogger, "drand"}, server, drandChain, nil)
2626

2727
assert.NoError(t, err)
2828
chain, err := c.FetchChainInfo(context.Background(), nil)

0 commit comments

Comments
 (0)