pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Automatically attach to non-propagating loggers

Open haxtibal opened this issue 3 years ago • 0 comments

Currently, pytest attaches its log handlers only to the root logger, and relies on log propagation to catch logs from loggers deeper in the hierarchy. In consequence we miss logs if one of those child loggers has logger.propagate set to False by user code. This affects caplog, report logging and live logging.

This commit showcases a possible fix, where we attach not only to the root logger, but also to non-propagating loggers as they occur. To do so we patch the standard libraries logging module with our own descriptor that intercepts assignments to logger.propagate. This enables us to inject and remove our pytest handlers right on time, even during executing test calls, with negligible performance impact.

Attempts to fix #3697.

PS: I'm aware the PR needs extra caution regarding thread safety and compatibility. Before considering such safe guards, I'd like to hear opinions on the approach in general. Feel free to close the PR right away if you think patching the standard library is against all rules.

haxtibal avatar Sep 16 '22 17:09 haxtibal