Skip to content

Conversation

PankajJaisu
Copy link

@PankajJaisu PankajJaisu commented Sep 6, 2025

Issue

Fix logging standardization in core modules (Phase 1)

Changes

  • Updated 7 core modules to use logging.getLogger(__name__)
  • Verified module-specific logging control works correctly
  • Ensured no import errors or functionality changes

Testing

1. Unit Tests

Added comprehensive unit tests to verify logger functionality:

pytest -v tests/core/test_libp2p/test_logger_names.py

Results:

  • test_logger_names: Verified all logger names are correct
  • test_loggers_exist_and_callable: Confirmed loggers work without errors

2. Module-Specific Logging Control

Network Module (Ping Example)

LIBP2P_DEBUG=network:DEBUG python examples/ping/ping.py

Observed logs:

(os_env) Pankajs-MacBook-Air:py-libp2p pankaj$ LIBP2P_DEBUG=network:DEBUG python examples/ping/ping.py

Logging to: /var/folders/tw/5t4_yn056c9658gtgjgzlpq00000gn/T/py-libp2p_20250922_173737_134600_1981fdb8.log
2025-09-22 17:37:37,716 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:37:37,716 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:37:37,716 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:37:37,716 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:37:37,716 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:37:37,716 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='0'
2025-09-22 17:37:37,728 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmXTphk65xqt91aUxp3KKGVcVM8Noz3KVaPsFg9Ub56oyu
2025-09-22 17:37:37,728 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 1220878f8c4732e940e314e6aa6482db12d59edf51c7968dbb819b8fe6ebabe23764
2025-09-22 17:37:37,731 [DEBUG] [async_service.Manager] <Manager[Swarm] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:37:37,731 - libp2p.network.swarm - DEBUG - Starting to listen
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='0'
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:37:37,731 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='0'
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='0'
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmXTphk65xqt91aUxp3KKGVcVM8Noz3KVaPsFg9Ub56oyu
2025-09-22 17:37:37,732 - libp2p.network.swarm - DEBUG - successfully started listening on: /ip4/0.0.0.0/tcp/0
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 1220878f8c4732e940e314e6aa6482db12d59edf51c7968dbb819b8fe6ebabe23764
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53306'
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 421
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: p2p
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_string] Input buffer: 1220878f8c4732e940e314e6aa6482db12d59edf51c7968dbb819b8fe6ebabe23764
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_string] Protocol: p2p
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_string] Expected codec: libp2p-key
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_string] Parsed as CIDv0: QmXTphk65xqt91aUxp3KKGVcVM8Noz3KVaPsFg9Ub56oyu
2025-09-22 17:37:37,732 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=p2p, value='QmXTphk65xqt91aUxp3KKGVcVM8Noz3KVaPsFg9Ub56oyu'
Run this from the same folder in another console:

ping-demo -d /ip4/0.0.0.0/tcp/53306/p2p/QmXTphk65xqt91aUxp3KKGVcVM8Noz3KVaPsFg9Ub56oyu

Waiting for incoming connection...

PubSub Module Example

LIBP2P_DEBUG=pubsub:DEBUG python examples/pubsub/pubsub.py

Observed logs:

(os_env) Pankajs-MacBook-Air:py-libp2p pankaj$ LIBP2P_DEBUG=pubsub:DEBUG python examples/pubsub/pubsub.py
Logging to: /var/folders/tw/5t4_yn056c9658gtgjgzlpq00000gn/T/py-libp2p_20250922_173812_380867_66d25134.log
2025-09-22 17:38:12,633 [INFO] [main] Running pubsub chat example...
2025-09-22 17:38:12,633 [INFO] [main] Your selected topic is: pubsub-chat
2025-09-22 17:38:12,633 [INFO] [main] Using random available port: 53311
2025-09-22 17:38:12,653 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmYBPunpnwUqrSV1GLR3vsXG4BE3fjJXvKujgQx7DqSXbj
2025-09-22 17:38:12,653 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 12209235e8c20ac3ecfc9dfcd72b78922559243643f9ff35a6089be7c0f61759d27e
2025-09-22 17:38:12,655 [DEBUG] [main] Host ID: QmYBPunpnwUqrSV1GLR3vsXG4BE3fjJXvKujgQx7DqSXbj
2025-09-22 17:38:12,655 [DEBUG] [main] Host multiselect protocols: ('/ipfs/id/1.0.0', '/ipfs/ping/1.0.0')
2025-09-22 17:38:12,655 - libp2p.pubsub.gossipsub - DEBUG - attached to pusub
2025-09-22 17:38:12,656 [DEBUG] [async_service.Manager] <Manager[Swarm] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53311'
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53311'
2025-09-22 17:38:12,656 [INFO] [main] Node started with peer ID: QmYBPunpnwUqrSV1GLR3vsXG4BE3fjJXvKujgQx7DqSXbj
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:38:12,656 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53311'
2025-09-22 17:38:12,656 [INFO] [main] Listening on: /ip4/0.0.0.0/tcp/53311
2025-09-22 17:38:12,656 [INFO] [main] Initializing PubSub and GossipSub...
2025-09-22 17:38:12,657 [DEBUG] [async_service.Manager] <Manager[Pubsub] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:38:12,657 [INFO] [main] Pubsub and GossipSub services started.
2025-09-22 17:38:12,657 [DEBUG] [async_service.Manager] <Manager[GossipSub] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:38:12,657 [INFO] [main] Pubsub ready.
2025-09-22 17:38:12,657 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmYBPunpnwUqrSV1GLR3vsXG4BE3fjJXvKujgQx7DqSXbj
2025-09-22 17:38:12,657 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 12209235e8c20ac3ecfc9dfcd72b78922559243643f9ff35a6089be7c0f61759d27e
2025-09-22 17:38:12,657 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmYBPunpnwUqrSV1GLR3vsXG4BE3fjJXvKujgQx7DqSXbj
2025-09-22 17:38:12,657 - libp2p.pubsub - DEBUG - subscribing to topic pubsub-chat
2025-09-22 17:38:12,658 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 12209235e8c20ac3ecfc9dfcd72b78922559243643f9ff35a6089be7c0f61759d27e
2025-09-22 17:38:12,661 [INFO] [main] Subscribed to topic: pubsub-chat
2025-09-22 17:38:12,660 - libp2p.pubsub.gossipsub - DEBUG - joining topic pubsub-chat
2025-09-22 17:38:12,661 [INFO] [main] Run this script in another console with:
pubsub-demo -d /ip4/127.0.0.1/tcp/53311/p2p/QmYBPunpnwUqrSV1GLR3vsXG4BE3fjJXvKujgQx7DqSXbj

2025-09-22 17:38:12,661 [INFO] [main] Waiting for peers...
2025-09-22 17:38:12,661 [DEBUG] [main] Starting publish loop...
Type messages to send (press Enter to send):
2025-09-22 17:38:12,661 [DEBUG] [main] Starting receive loop

Combined Module Logging

LIBP2P_DEBUG=network:DEBUG,pubsub:INFO python examples/pubsub/pubsub.py

(os_env) Pankajs-MacBook-Air:py-libp2p pankaj$ LIBP2P_DEBUG=network:DEBUG,pubsub:INFO python examples/pubsub/pubsub.py

Observed logs:
Logging to: /var/folders/tw/5t4_yn056c9658gtgjgzlpq00000gn/T/py-libp2p_20250922_174035_520584_706396ee.log
2025-09-22 17:40:35,635 [INFO] [main] Running pubsub chat example...
2025-09-22 17:40:35,635 [INFO] [main] Your selected topic is: pubsub-chat
2025-09-22 17:40:35,636 [INFO] [main] Using random available port: 53554
2025-09-22 17:40:35,655 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmbChtqBGeZwZACNj7LLJCiereteSJc53wLVfjcVELwQ6A
2025-09-22 17:40:35,655 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 1220bf1ee27f9fad2667b8ec5033542ed64aab5dbf4147e9792196ec47982bca91a7
2025-09-22 17:40:35,657 [DEBUG] [main] Host ID: QmbChtqBGeZwZACNj7LLJCiereteSJc53wLVfjcVELwQ6A
2025-09-22 17:40:35,657 [DEBUG] [main] Host multiselect protocols: ('/ipfs/id/1.0.0', '/ipfs/ping/1.0.0')
2025-09-22 17:40:35,657 [DEBUG] [async_service.Manager] <Manager[Swarm] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:40:35,657 - libp2p.network.swarm - DEBUG - Starting to listen
2025-09-22 17:40:35,657 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:40:35,657 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53554'
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53554'
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53554'
2025-09-22 17:40:35,658 [INFO] [main] Node started with peer ID: QmbChtqBGeZwZACNj7LLJCiereteSJc53wLVfjcVELwQ6A
2025-09-22 17:40:35,658 - libp2p.network.swarm - DEBUG - successfully started listening on: /ip4/0.0.0.0/tcp/53554
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: ip4
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=ip4, value='0.0.0.0'
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Decoded protocol code: 6
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG bytes_to_string] Protocol name: tcp
2025-09-22 17:40:35,658 [DEBUG] [multiaddr.transforms] [DEBUG] bytes_to_string: proto=tcp, value='53554'
2025-09-22 17:40:35,658 [INFO] [main] Listening on: /ip4/0.0.0.0/tcp/53554
2025-09-22 17:40:35,658 [INFO] [main] Initializing PubSub and GossipSub...
2025-09-22 17:40:35,659 [DEBUG] [async_service.Manager] <Manager[Pubsub] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:40:35,659 [DEBUG] [async_service.Manager] <Manager[GossipSub] flags=SRcfe>: _handle_cancelled waiting for cancellation
2025-09-22 17:40:35,659 [INFO] [main] Pubsub and GossipSub services started.
2025-09-22 17:40:35,659 [INFO] [main] Pubsub ready.
2025-09-22 17:40:35,659 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmbChtqBGeZwZACNj7LLJCiereteSJc53wLVfjcVELwQ6A
2025-09-22 17:40:35,659 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 1220bf1ee27f9fad2667b8ec5033542ed64aab5dbf4147e9792196ec47982bca91a7
2025-09-22 17:40:35,659 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Input value: QmbChtqBGeZwZACNj7LLJCiereteSJc53wLVfjcVELwQ6A
2025-09-22 17:40:35,659 [DEBUG] [multiaddr.codecs.cid] [DEBUG CID to_bytes] Parsed as CIDv0: 1220bf1ee27f9fad2667b8ec5033542ed64aab5dbf4147e9792196ec47982bca91a7
2025-09-22 17:40:35,660 [INFO] [main] Subscribed to topic: pubsub-chat
2025-09-22 17:40:35,660 [INFO] [main] Run this script in another console with:
pubsub-demo -d /ip4/127.0.0.1/tcp/53554/p2p/QmbChtqBGeZwZACNj7LLJCiereteSJc53wLVfjcVELwQ6A

2025-09-22 17:40:35,660 [INFO] [main] Waiting for peers...
2025-09-22 17:40:35,660 [DEBUG] [main] Starting receive loop
2025-09-22 17:40:35,660 [DEBUG] [main] Starting publish loop...
Type messages to send (press Enter to send):

3. Logger Name Verification

python -c "
import libp2p.network.swarm
import libp2p.pubsub.gossipsub
print(f'Swarm logger: {libp2p.network.swarm.logger.name}')
print(f'GossipSub logger: {libp2p.pubsub.gossipsub.logger.name}')
"

Output:

Swarm logger: libp2p.network.swarm
GossipSub logger: libp2p.pubsub.gossipsub

4. Basic Functionality Test

cd examples/ping/
python ping.py
  • No import errors observed
  • All existing functionality works as expected
  • No breaking changes introduced

Files Modified

Network & Host Modules

  • libp2p/network/swarm.py: logging.getLogger("libp2p.network.swarm")logging.getLogger(__name__)
  • libp2p/host/basic_host.py: logging.getLogger("libp2p.network.basic_host")logging.getLogger(__name__)
  • libp2p/transport/tcp/tcp.py: logging.getLogger("libp2p.transport.tcp")logging.getLogger(__name__)

PubSub Modules

  • libp2p/pubsub/floodsub.py: logging.getLogger("libp2p.pubsub.floodsub")logging.getLogger(__name__)
  • libp2p/pubsub/gossipsub.py: logging.getLogger("libp2p.pubsub.gossipsub")logging.getLogger(__name__)
  • libp2p/pubsub/pubsub.py: logging.getLogger("libp2p.pubsub")logging.getLogger(__name__)
  • libp2p/pubsub/validators.py: logging.getLogger("libp2p.pubsub")logging.getLogger(__name__)

Verification Checklist

  • All 7 target files updated with logging.getLogger(__name__)
  • Unit tests added and passing
  • Module-specific logging control verified with LIBP2P_DEBUG
  • Logger names confirmed to match module paths
  • Basic functionality tests pass
  • No breaking changes to existing logging behavior
  • No import errors or functionality regressions

Learning

Working on this issue provided valuable insights into:

  • py-libp2p's logging architecture: Understanding how the custom logging system in libp2p/utils/logging.py enables module-specific control via LIBP2P_DEBUG
  • Logger hierarchy importance: How proper logger naming enables granular debugging control across the entire library
  • Benefits of __name__: Automatic module path detection eliminates hardcoded strings and ensures consistency
  • Testing strategies: Balancing unit tests with integration testing for logging systems

Impact

After these changes, developers now have:

  • Granular logging control: LIBP2P_DEBUG=network:DEBUG,pubsub:INFO
  • Consistent logger hierarchy: All core modules follow the same naming pattern
  • Improved debugging experience: Module-specific logs make troubleshooting more efficient
  • Foundation for Phase 2/3: Standardized approach ready for discovery, relay, and DHT modules

References

@acul71
Copy link
Contributor

acul71 commented Sep 20, 2025

@PankajJaisu

PR Review: #902 - Logger Standardization Phase 1

Reviewer: Luca
Date: September 19, 2025
PR: fix: standardize logger names in core modules (phase 1) #902
Author: @PankajJaisu

Summary

This PR implements the first phase of logger standardization across core py-libp2p modules by replacing hardcoded logger names with logging.getLogger(__name__). The changes affect 7 core modules and include comprehensive test coverage.

Changes Overview

Files Modified

  • libp2p/network/swarm.py
  • libp2p/host/basic_host.py
  • libp2p/transport/tcp/tcp.py
  • libp2p/pubsub/floodsub.py
  • libp2p/pubsub/gossipsub.py
  • libp2p/pubsub/validators.py
  • examples/pubsub/pubsub.py

Test Files Added

  • tests/core/test_libp2p/test_logger_names.py

Detailed Analysis

Strengths

  1. Correct Implementation: The change from hardcoded strings to __name__ is implemented correctly across all files. This ensures logger names automatically match the module path.

  2. Comprehensive Test Coverage: The test file test_logger_names.py provides excellent coverage:

    • Verifies logger names match expected module paths
    • Tests logger functionality and callability
    • Includes manual verification function for debugging
  3. No Breaking Changes: The changes maintain backward compatibility. Logger names remain the same (e.g., libp2p.network.swarm), just the method of obtaining them is improved.

  4. Good Documentation: The PR description is thorough and includes:

    • Clear explanation of changes
    • Testing methodology
    • Verification steps
    • Impact assessment
  5. Proper Testing: The author tested the LIBP2P_DEBUG environment variable functionality, which is crucial for the py-libp2p logging system.

Verification Results

I verified the following aspects:

  1. Logger Names: ✅ Confirmed correct naming

    Swarm logger: libp2p.network.swarm
    GossipSub logger: libp2p.pubsub.gossipsub
  2. Test Suite: ✅ All tests pass

    tests/core/test_libp2p/test_logger_names.py::test_logger_names PASSED
    tests/core/test_libp2p/test_logger_names.py::test_loggers_exist_and_callable PASSED
    
  3. LIBP2P_DEBUG Functionality: ✅ Module-specific logging works

    LIBP2P_DEBUG=network:DEBUG python -c "..."
    # Output: 2025-09-19 21:52:13,912 - libp2p.network.swarm - DEBUG - Test debug message

⚠️ Minor Issues

  1. Inconsistent Logger Name in basic_host.py:

    • The file is located at libp2p/host/basic_host.py
    • But the old logger name was "libp2p.network.basic_host" (incorrectly referenced network)
    • The new logger name will be "libp2p.host.basic_host" (correct)
    • This is actually a fix for an existing bug, but should be highlighted
  2. Missing pubsub.py in Core Changes:

    • The PR mentions updating libp2p/pubsub/pubsub.py but it's not in the main commit
    • Only examples/pubsub/pubsub.py is shown in the diff
    • This should be clarified

🔍 Code Quality Assessment

Before:

logger = logging.getLogger("libp2p.network.swarm")

After:

logger = logging.getLogger(__name__)

This change is:

  • ✅ More maintainable (no hardcoded strings)
  • ✅ Less error-prone (automatic module path detection)
  • ✅ Consistent with Python best practices
  • ✅ Future-proof (works if modules are moved/renamed)

📊 Impact Analysis

Positive Impacts:

  • Improved maintainability across the codebase
  • Consistent logging approach
  • Better foundation for future phases
  • Fixed incorrect logger name in basic_host.py

Risk Assessment:

  • Low Risk: No functional changes, only logger instantiation method
  • No Breaking Changes: Logger names remain identical
  • Backward Compatible: Existing logging configuration continues to work

🧪 Testing Assessment

The testing approach is comprehensive:

  1. Unit Tests: ✅ Proper test coverage for logger functionality
  2. Integration Tests: ✅ Verified LIBP2P_DEBUG environment variable
  3. Manual Verification: ✅ Included helper function for debugging
  4. Cross-Module Testing: ✅ Tested both network and pubsub modules

📝 Recommendations

  1. Approve with Minor Comments: This PR is well-implemented and ready for merge.

  2. Document the basic_host.py Fix: Consider adding a note about fixing the incorrect logger name in the commit message.

  3. Consider Adding More Test Cases: Could add tests for:

    • Logger hierarchy verification
    • Error handling in logger creation
    • Performance impact (minimal, but good to document)
  4. Future Phases: The approach established here provides a solid foundation for phases 2 and 3.

🎯 Final Verdict

APPROVE

This PR successfully implements logger standardization for core modules with:

  • Correct technical implementation
  • Comprehensive testing
  • Good documentation
  • No breaking changes
  • Fixes an existing bug in basic_host.py

The changes align with Python best practices and improve the maintainability of the py-libp2p codebase. The author has demonstrated thorough testing and understanding of the logging system.

📋 Action Items for Author

  1. Consider documenting the basic_host.py logger name fix in the commit message
  2. Clarify if libp2p/pubsub/pubsub.py was intended to be included in this phase
  3. Excellent work on the comprehensive testing approach!

Review Status:APPROVED
Confidence Level: High
Risk Level: Low

@acul71
Copy link
Contributor

acul71 commented Sep 20, 2025

@PankajJaisu can you add a newsfragment ?

@seetadev
Copy link
Contributor

@PankajJaisu : please respond to @acul71. Appreciate your initiative.

Please try and complete this PR before the maintainer's call this week. We plan to do a py-libp2p release once websockets support is added and the corresponding PR merged. Would like your opened PRs to be reviewed, merged and landed in the new release too.

@PankajJaisu
Copy link
Author

@seetadev @acul71 I have added new fragment as per the review @acul71 can you please check and let me know if there are any other issue as well

@acul71
Copy link
Contributor

acul71 commented Sep 22, 2025

@acul71 I have added new fragment

Can't find it

/py-libp2p$ l newsfragments/906*
ls: cannot access 'newsfragments/906*': No such file or directory

@acul71
Copy link
Contributor

acul71 commented Sep 22, 2025

@seetadev
Can you re-run CI/CD ?

@seetadev
Copy link
Contributor

@acul71 : Did run the CI/Cd pipeline. Taking considerable time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants