Improve Logging Configuration: don't modify root logger #182
+14
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, pytest-mpl uses
logging.basicConfiguration
to configure logging. But this modifies the configuration of the root logger which leads to unexpected and unwanted behaviour when testing code that itself useslogging
.To demonstrate the problem, I will use
test_succeeds_faulty_mirror()
with an additional logging call added:This test already causes a logging message from pytest-mpl itself, because an invalid URL is used. Now addtionally, it simulates that the tested code/module makes use of
logging
as well.In pytest the
--log-cli-level
option can be used to configure the logging level that is output on the cli. By default, no messages are output.If we now run the above example...
... without pytest-mpl (
pytest
) there is no logging output as expected.... with pytest-mpl (
pytest --mpl
), suddenly there is logging output from pytest-mpl and from the tested code (should not happen).... with pytest-mpl and logging configured through pytest (
pytest --mpl --log-cli-level=INFO
), the logging messages from pytest-mpl are now output twice. (This happens because messages are propagated to the root logger as well and those are then handled by pytest.)The main problem here is that it is impossible to turn of logging to the cli when pytest-mpl is used. As shown, this is usually configured in pytest with the
--log-cli-level
option and similar options. But pytest-mpl's modification of the root logger completely bypasses this. This can make test output fairly unreadable if the tested module itself makes extensive use oflogging
.This PR implements the usage of a completely separate logger for pytest-mpl. Its logging configuration is fully independent of pytest and independent of the tested code. Additionally, the same logging format as used for pytest's cli ouput is used.
I did consider tying into pytest's logging functionality directly to make pytest-mpl interact perfectly and use all features, including coloured output and similar. But this would need to rely heavily on non-public API parts of pytest and might therefore be prone to breaking when there are changes in pytest. The minor improvements would not be worth the risk and maintenance effort in my opinion.