Skip to content

Commit 9238785

Browse files
Refactor logging to use logtrace package across multiple modules
1 parent 087529f commit 9238785

File tree

31 files changed

+1088
-536
lines changed

31 files changed

+1088
-536
lines changed

p2p/kademlia/bootstrap.go

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/LumeraProtocol/supernode/pkg/errors"
1313
"github.com/LumeraProtocol/supernode/pkg/utils"
1414

15-
"github.com/LumeraProtocol/supernode/pkg/log"
15+
"github.com/LumeraProtocol/supernode/pkg/logtrace"
1616
ltc "github.com/LumeraProtocol/supernode/pkg/net/credentials"
1717
)
1818

@@ -65,9 +65,11 @@ func (s *DHT) parseNode(extP2P string, selfAddr string) (*Node, error) {
6565
// For system testing, use port+1 if SYSTEM_TEST=true
6666
if os.Getenv("SYSTEM_TEST") == "true" {
6767
port = uint16(portNum) + 1
68-
log.P2P().WithField("original_port", portNum).
69-
WithField("adjusted_port", port).
70-
Info("Using port+1 for system testing")
68+
logtrace.Info(context.Background(), "Using port+1 for system testing", logtrace.Fields{
69+
logtrace.FieldModule: "p2p",
70+
"original_port": portNum,
71+
"adjusted_port": port,
72+
})
7173
} else {
7274
// For normal P2P operation, always use the default port
7375
port = defaultNetworkPort
@@ -108,7 +110,10 @@ func (s *DHT) setBootstrapNodesFromConfigVar(ctx context.Context, bootstrapNodes
108110
})
109111
}
110112
s.options.BootstrapNodes = nodes
111-
log.P2P().WithContext(ctx).WithField("bootstrap_nodes", nodes).Info("Bootstrap nodes set from config var")
113+
logtrace.Info(ctx, "Bootstrap nodes set from config var", logtrace.Fields{
114+
logtrace.FieldModule: "p2p",
115+
"bootstrap_nodes": nodes,
116+
})
112117

113118
return nil
114119
}
@@ -158,15 +163,22 @@ func (s *DHT) ConfigureBootstrapNodes(ctx context.Context, bootstrapNodes string
158163
}
159164

160165
if latestIP == "" {
161-
log.P2P().WithContext(ctx).WithField("supernode", supernode.SupernodeAccount).Warn("No valid IP address found for supernode")
166+
logtrace.Warn(ctx, "No valid IP address found for supernode", logtrace.Fields{
167+
logtrace.FieldModule: "p2p",
168+
"supernode": supernode.SupernodeAccount,
169+
})
162170
continue
163171
}
164172

165173
// Parse the node from the IP address
166174
node, err := s.parseNode(latestIP, selfAddress)
167175
if err != nil {
168-
log.P2P().WithContext(ctx).WithError(err).WithField("address", latestIP).WithField("supernode", supernode.SupernodeAccount).
169-
Warn("Skip Bad Bootstrap Address")
176+
logtrace.Warn(ctx, "Skip Bad Bootstrap Address", logtrace.Fields{
177+
logtrace.FieldModule: "p2p",
178+
logtrace.FieldError: err.Error(),
179+
"address": latestIP,
180+
"supernode": supernode.SupernodeAccount,
181+
})
170182
continue
171183
}
172184

@@ -185,13 +197,19 @@ func (s *DHT) ConfigureBootstrapNodes(ctx context.Context, bootstrapNodes string
185197
}
186198

187199
if len(boostrapNodes) == 0 {
188-
log.WithContext(ctx).Error("unable to fetch bootstrap IP addresses. No valid supernodes found.")
200+
logtrace.Error(ctx, "unable to fetch bootstrap IP addresses. No valid supernodes found.", logtrace.Fields{
201+
logtrace.FieldModule: "p2p",
202+
})
189203
return nil
190204
}
191205

192206
for _, node := range boostrapNodes {
193-
log.WithContext(ctx).WithFields(log.Fields{"bootstap_ip": node.IP, "bootstrap_port": node.Port, "node_id": string(node.ID)}).
194-
Info("adding p2p bootstrap node")
207+
logtrace.Info(ctx, "adding p2p bootstrap node", logtrace.Fields{
208+
logtrace.FieldModule: "p2p",
209+
"bootstap_ip": node.IP,
210+
"bootstrap_port": node.Port,
211+
"node_id": string(node.ID),
212+
})
195213
}
196214

197215
s.options.BootstrapNodes = append(s.options.BootstrapNodes, boostrapNodes...)
@@ -221,7 +239,10 @@ func (s *DHT) Bootstrap(ctx context.Context, bootstrapNodes string) error {
221239

222240
addr := fmt.Sprintf("%s:%v", node.IP, node.Port)
223241
if _, err := s.cache.Get(addr); err == nil {
224-
log.P2P().WithContext(ctx).WithField("addr", addr).Info("skip bad p2p boostrap addr")
242+
logtrace.Info(ctx, "skip bad p2p boostrap addr", logtrace.Fields{
243+
logtrace.FieldModule: "p2p",
244+
"addr": addr,
245+
})
225246
continue
226247
}
227248

@@ -245,20 +266,32 @@ func (s *DHT) Bootstrap(ctx context.Context, bootstrapNodes string) error {
245266
// So if bootstrap failed, should try to connect to node again for next bootstrap retry
246267
// s.cache.SetWithExpiry(addr, []byte("true"), badAddrExpiryHours*time.Hour)
247268

248-
log.P2P().WithContext(ctx).WithError(err).Error("network call failed, sleeping 3 seconds")
269+
logtrace.Error(ctx, "network call failed, sleeping 3 seconds", logtrace.Fields{
270+
logtrace.FieldModule: "p2p",
271+
logtrace.FieldError: err.Error(),
272+
})
249273
time.Sleep(5 * time.Second)
250274
continue
251275
}
252-
log.P2P().WithContext(ctx).Debugf("ping response: %v", response.String())
276+
logtrace.Debug(ctx, "ping response", logtrace.Fields{
277+
logtrace.FieldModule: "p2p",
278+
"response": response.String(),
279+
})
253280

254281
// add the node to the route table
255-
log.P2P().WithContext(ctx).WithField("sender-id", string(response.Sender.ID)).
256-
WithField("sender-ip", string(response.Sender.IP)).
257-
WithField("sender-port", response.Sender.Port).Debug("add-node params")
282+
logtrace.Debug(ctx, "add-node params", logtrace.Fields{
283+
logtrace.FieldModule: "p2p",
284+
"sender-id": string(response.Sender.ID),
285+
"sender-ip": string(response.Sender.IP),
286+
"sender-port": response.Sender.Port,
287+
})
258288

259289
if len(response.Sender.ID) != len(s.ht.self.ID) {
260-
log.P2P().WithContext(ctx).WithField("sender-id", string(response.Sender.ID)).
261-
WithField("self-id", string(s.ht.self.ID)).Error("self ID && sender ID len don't match")
290+
logtrace.Error(ctx, "self ID && sender ID len don't match", logtrace.Fields{
291+
logtrace.FieldModule: "p2p",
292+
"sender-id": string(response.Sender.ID),
293+
"self-id": string(s.ht.self.ID),
294+
})
262295

263296
continue
264297
}
@@ -277,7 +310,10 @@ func (s *DHT) Bootstrap(ctx context.Context, bootstrapNodes string) error {
277310
if s.ht.totalCount() > 0 {
278311
// iterative find node from the nodes
279312
if _, err := s.iterate(ctx, IterateFindNode, s.ht.self.ID, nil, 0); err != nil {
280-
log.P2P().WithContext(ctx).WithError(err).Error("iterative find node failed")
313+
logtrace.Error(ctx, "iterative find node failed", logtrace.Fields{
314+
logtrace.FieldModule: "p2p",
315+
logtrace.FieldError: err.Error(),
316+
})
281317
return err
282318
}
283319
} else {
@@ -291,12 +327,18 @@ func (s *DHT) Bootstrap(ctx context.Context, bootstrapNodes string) error {
291327

292328
func (s *DHT) retryBootstrap(ctx context.Context, bootstrapNodes string) {
293329
if err := s.ConfigureBootstrapNodes(ctx, bootstrapNodes); err != nil {
294-
log.P2P().WithContext(ctx).WithError(err).Error("retry failed to get bootstap ip")
330+
logtrace.Error(ctx, "retry failed to get bootstap ip", logtrace.Fields{
331+
logtrace.FieldModule: "p2p",
332+
logtrace.FieldError: err.Error(),
333+
})
295334
return
296335
}
297336

298337
// join the kademlia network if bootstrap nodes is set
299338
if err := s.Bootstrap(ctx, bootstrapNodes); err != nil {
300-
log.P2P().WithContext(ctx).WithError(err).Error("retry failed - bootstrap the node.")
339+
logtrace.Error(ctx, "retry failed - bootstrap the node.", logtrace.Fields{
340+
logtrace.FieldModule: "p2p",
341+
logtrace.FieldError: err.Error(),
342+
})
301343
}
302344
}

0 commit comments

Comments
 (0)