Skip to content

Fix logging setup when python-rich is not present#445

Merged
slyon merged 1 commit intocanonical:mainfrom
frhuelsz:fix-loggin-no-rich
Mar 13, 2024
Merged

Fix logging setup when python-rich is not present#445
slyon merged 1 commit intocanonical:mainfrom
frhuelsz:fix-loggin-no-rich

Conversation

@frhuelsz
Copy link
Contributor

@frhuelsz frhuelsz commented Mar 2, 2024

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:

  • The logging level will be WARNING instead of INFO
  • Passing --debug has no effect.

Explanation

When python-rich is not present, the import in netplan_cli/cli/commands/status.py#L35 will fail, causing the except clause to run which calls logging.debug(...) in netplan_cli/cli/commands/status.py#L47

However, 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(...) in status.py does a hidden call to basicConfig() so that the log can go somewhere: https://github.com/python/cpython/blob/5dc8c84d397110f9edfa56793ad8887b1f176d79/Lib/logging/__init__.py#L2181

This hidden call to basicConfig() creates a handler on the root logger. (The root logger is set up to WARNING by default, so this log will never show up under normal conditions.)

When basicConfig(...) is called in core.py, the call will have no effect because a handler already exists. From the docs:

This function does nothing if the root logger already has handlers configured, unless the keyword argument force is set to True.
(https://docs.python.org/3/library/logging.html#logging.basicConfig)

Hence this PR to use force=True and 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

  • Runs make check successfully.
  • Retains 100% code coverage (make check-coverage).
  • New/changed keys in YAML format are documented.
  • (Optional) Adds example YAML for new feature.
  • (Optional) Closes an open bug in Launchpad.

@slyon slyon added community This PR has been proposed by somebody outside of the Netplan team and roadmap commitments. stable Might be merged in a stable branch labels Mar 11, 2024
Copy link
Contributor

@slyon slyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution to Netplan and the detailed analysis.

LGTM.

@slyon slyon merged commit 368eff3 into canonical:main Mar 13, 2024
@slyon slyon removed the stable Might be merged in a stable branch label Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community This PR has been proposed by somebody outside of the Netplan team and roadmap commitments.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants