Skip to content

Commit 880992d

Browse files
lidelMarcoPolo
authored andcommitted
feat: improve NAT logging clarity
- Gateway discovery now logs at INFO (success) or WARN (error) - Port mapping lifecycle visible at INFO level - Periodic refreshes only log at DEBUG to reduce noise
1 parent 2517e5a commit 880992d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

p2p/net/nat/nat.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ func DiscoverNAT(ctx context.Context) (*NAT, error) {
6262
// Log the device addr.
6363
addr, err := natInstance.GetDeviceAddress()
6464
if err != nil {
65-
log.Debug("DiscoverGateway address error:", err)
65+
log.Warn("DiscoverGateway address error:", err)
6666
} else {
67-
log.Debug("DiscoverGateway address:", addr)
67+
log.Info("DiscoverGateway address:", addr)
6868
}
6969

7070
ctx, cancel := context.WithCancel(context.Background())
@@ -169,6 +169,8 @@ func (nat *NAT) AddMapping(ctx context.Context, protocol string, port int) error
169169
return nil
170170
}
171171

172+
log.Infof("Starting maintenance of port mapping: %s/%d", protocol, port)
173+
172174
// do it once synchronously, so first mapping is done right away, and before exiting,
173175
// allowing users -- in the optimistic case -- to use results right after.
174176
extPort := nat.establishMapping(ctx, protocol, port)
@@ -191,6 +193,7 @@ func (nat *NAT) RemoveMapping(ctx context.Context, protocol string, port int) er
191193
case "tcp", "udp":
192194
e := entry{protocol: protocol, port: port}
193195
if _, ok := nat.mappings[e]; ok {
196+
log.Infof("Stopping maintenance of port mapping: %s/%d", protocol, port)
194197
delete(nat.mappings, e)
195198
return nat.nat.DeletePortMapping(ctx, protocol, port)
196199
}
@@ -292,7 +295,7 @@ func (nat *NAT) establishMapping(ctx context.Context, protocol string, internalP
292295
// Handle success
293296
if err == nil && externalPort != 0 {
294297
nat.consecutiveFailures = 0
295-
log.Infof("NAT port mapping established: %s %d -> %d", protocol, internalPort, externalPort)
298+
log.Debugf("NAT port mapping established: %s %d -> %d", protocol, internalPort, externalPort)
296299
return externalPort
297300
}
298301

0 commit comments

Comments
 (0)