user2690730
user2690730

Reputation: 273

Why are loggers not propogating when I run pytest

I'm unable to get logging output from my unit tests and it looks like the reason is that all loggers except for the root logger have propagate set to false when I run pytest. I can run the following file, using pytest and python test_file.py. In the former logging.getLogger('one').propagate == True while in the latter logging.getLogger('one').propagate == False. Why is this?

test_file.py

import logging

def test_function():
  print()
  print('root', logging.getLogger().propagate)
  print('one', logging.getLogger('one').propagate)

if __name__ == '__main__':
  test_function()

How do I get all my loggers to propagate? Searching the internet only turns up questions about how to turn off propagation as if most people have the opposite experience that I do.

Upvotes: 2

Views: 577

Answers (1)

user2690730
user2690730

Reputation: 273

This isn't a problem with pytest. I searched the cpython and pytest source code for anything that would do this and found nothing. Then I stepped through the getLogger code and found that something was calling the logging.setLoggerClass function and replacing the logger class with a subclass that sets propagate to false. I set a breakpoint on setLoggerClass and found that a pytest plugin provided by ROS2 was doing this. I added the following to pytest.ini and now everything works great.

[pytest]
addopts = -p no:launch -p no:launch_ros

Upvotes: 2

Related Questions