mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-23 17:14:22 +01:00
pyln: Remove any logging handlers at teardown to avoid logging error
Inspired by https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873
This commit is contained in:
@@ -40,14 +40,25 @@ def test_base_dir():
|
|||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def setup_logging():
|
def setup_logging():
|
||||||
logger = logging.getLogger()
|
"""Enable logging before a test, and remove all handlers afterwards.
|
||||||
before_handlers = list(logger.handlers)
|
|
||||||
|
|
||||||
|
This "fixes" the issue with pytest swapping out sys.stdout and sys.stderr
|
||||||
|
in order to capture the output, but then doesn't wait for the handlers to
|
||||||
|
terminate before closing the buffers. It just iterates through all
|
||||||
|
loggers, and removes any handlers that might be pointing at sys.stdout or
|
||||||
|
sys.stderr.
|
||||||
|
|
||||||
|
"""
|
||||||
if TEST_DEBUG:
|
if TEST_DEBUG:
|
||||||
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
|
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
|
||||||
|
|
||||||
yield
|
yield
|
||||||
logger.handlers = before_handlers
|
|
||||||
|
loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values())
|
||||||
|
for logger in loggers:
|
||||||
|
handlers = getattr(logger, 'handlers', [])
|
||||||
|
for handler in handlers:
|
||||||
|
logger.removeHandler(handler)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
Reference in New Issue
Block a user