Fix logging setup when python-rich is not present #445
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.
Description
Hi, while packaging netplan 0.107 for Azure Linux I ran into this small bug when running checks. This seems like the most trivial fix.
tl;dr: when python-rich is not present the logger will not be configured correctly. More specifically:
WARNINGinstead ofINFO--debughas no effect.Explanation
When python-rich is not present, the import in netplan_cli/cli/commands/status.py#L35 will fail, causing the
exceptclause to run which callslogging.debug(...)in netplan_cli/cli/commands/status.py#L47However, because this is evaluated when the modules are being imported, this happens before the code in netplan_cli/cli/core.py#L51 that calls
logging.basicConfig(...).Because the root logger has not been initialized, the call to
logging.debug(...)instatus.pydoes a hidden call tobasicConfig()so that the log can go somewhere: https://github.com/python/cpython/blob/5dc8c84d397110f9edfa56793ad8887b1f176d79/Lib/logging/__init__.py#L2181This hidden call to
basicConfig()creates a handler on the root logger. (The root logger is set up toWARNINGby default, so this log will never show up under normal conditions.)When
basicConfig(...)is called incore.py, the call will have no effect because a handler already exists. From the docs:Hence this PR to use
force=Trueand avoid the issue.Another possible solution is to remove the call from
status.py, which will not be seen anyway.Thanks for your time. :)
Checklist
make checksuccessfully.make check-coverage).