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:

  • libp2p.network.swarm logger correctly outputs debug messages
  • Listening started and connection information displayed successfully
  • Module-specific debug control working as expected

PubSub Module Example

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

Observed logs:

  • libp2p.pubsub.gossipsub logger correctly outputs debug messages
  • Node started, topic subscription succeeded, and gossip messages logged
  • PubSub debug control functioning properly

Combined Module Logging

LIBP2P_DEBUG=network:DEBUG,pubsub:INFO python examples/pubsub/pubsub.py
  • Network modules show DEBUG level messages
  • PubSub modules show INFO+ level messages
  • Hierarchical logging control verified

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

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.

1 participant