Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHELC-1732] refactor: Improved logger usage #1327

Merged
merged 2 commits into from
Sep 11, 2024

Conversation

Venefilyn
Copy link
Member

@Venefilyn Venefilyn commented Aug 2, 2024

This refactor of the usage of our logging is to get away from custom
functions that causes attribute errors in editors and IDEs. This will
also standarize the usage of logger and the loglevels.

In future PRs we can include changes to include prepare, rollback, and
final specific message styles.

Jira Issues:

Checklist

  • PR has been tested manually in a VM (either author or reviewer)
  • Jira issue has been made public if possible
  • [RHELC-] or [HMS-] is part of the PR title
  • GitHub label has been added to help with Release notes
  • PR title explains the change from the user's point of view
  • Code and tests are documented properly
  • The commits are squashed to as few commits as possible (without losing data)
  • When merged: Jira issue has been updated to Release Pending if relevant

@Venefilyn Venefilyn marked this pull request as draft August 2, 2024 13:00
@Venefilyn Venefilyn force-pushed the refactor/logger branch 2 times, most recently from 246d94f to d97b77e Compare August 19, 2024 15:17
Copy link

codecov bot commented Aug 19, 2024

Codecov Report

Attention: Patch coverage is 96.11872% with 17 lines in your changes missing coverage. Please review.

Project coverage is 96.34%. Comparing base (6722626) to head (11b8e34).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
convert2rhel/subscription.py 94.59% 4 Missing ⚠️
convert2rhel/logger.py 93.18% 1 Missing and 2 partials ⚠️
convert2rhel/utils/__init__.py 90.90% 3 Missing ⚠️
convert2rhel/pkghandler.py 90.47% 2 Missing ⚠️
convert2rhel/backup/packages.py 95.00% 1 Missing ⚠️
convert2rhel/breadcrumbs.py 83.33% 1 Missing ⚠️
convert2rhel/pkgmanager/handlers/yum/__init__.py 96.00% 1 Missing ⚠️
convert2rhel/redhatrelease.py 83.33% 1 Missing ⚠️
convert2rhel/toolopts.py 95.45% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1327      +/-   ##
==========================================
- Coverage   96.43%   96.34%   -0.10%     
==========================================
  Files          66       66              
  Lines        4997     4983      -14     
  Branches      877      877              
==========================================
- Hits         4819     4801      -18     
- Misses        101      102       +1     
- Partials       77       80       +3     
Flag Coverage Δ
centos-linux-7 91.76% <89.72%> (-0.11%) ⬇️
centos-linux-8 92.64% <90.86%> (-0.11%) ⬇️
centos-linux-9 92.69% <90.86%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Venefilyn Venefilyn marked this pull request as ready for review August 19, 2024 15:22
@Venefilyn Venefilyn requested review from a team as code owners August 19, 2024 15:22
@Venefilyn Venefilyn requested a review from SerCantus August 19, 2024 15:22
@Venefilyn Venefilyn changed the title Standardize logger usage refactor: Improved logger usage Aug 19, 2024
@Venefilyn Venefilyn added the skip/changelog If it should be excluded from changelog or Release notes. Such as infra, reverted PRs, etc. label Aug 19, 2024
@Venefilyn
Copy link
Member Author

Ready for review and testing whenever (wanted it to be ready before I leave for PTO)

Comment on lines +326 to +349
# get root logger
logging.setLoggerClass(CustomLogger)
root_logger = logging.getLogger("convert2rhel") # type: CustomLogger # type: ignore
Copy link
Member Author

@Venefilyn Venefilyn Aug 20, 2024

Choose a reason for hiding this comment

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

This can look a bit confusing but essentially we set it to a type explicitly, but since getLogger() just returns a normal Logger class even if we set our own we have to ignore it as well. It is something that could be improved with more code if it doesn't work for everyone, but with this we get what we want and it should be safe given how the setLoggerClass() works

This implementation is a bit different from how the demo (recording exists) that I had August 13th

Copy link
Member

Choose a reason for hiding this comment

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

Looks fine for me. Not a single problem

@Venefilyn Venefilyn force-pushed the refactor/logger branch 3 times, most recently from 8e56069 to c01f078 Compare August 21, 2024 17:51
@Venefilyn Venefilyn force-pushed the refactor/logger branch 3 times, most recently from 75a55dd to 9ad27b6 Compare September 10, 2024 11:30
@Venefilyn Venefilyn force-pushed the refactor/logger branch 3 times, most recently from b51f620 to a9c80df Compare September 10, 2024 11:50
@Venefilyn Venefilyn changed the title refactor: Improved logger usage [RHELC-1732] refactor: Improved logger usage Sep 10, 2024
@Venefilyn Venefilyn mentioned this pull request Sep 10, 2024
8 tasks
@SerCantus SerCantus added the tests/sanity PR ready to run the sanity test suit. Equivalent to `/packit test --labels sanity`. label Sep 10, 2024
@has-bot
Copy link
Member

has-bot commented Sep 10, 2024

/packit test --labels sanity


Comment generated by an automation.

Copy link
Member

@SerCantus SerCantus left a comment

Choose a reason for hiding this comment

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

Awesome refactor @Venefilyn 🥇 . I added the tests-run-sanity label to the PR.

@SerCantus
Copy link
Member

Looks like sanity checks are failing as the test doesn't recognise the convert2rhel module importing root_logger in tests/integration/conftest.py. Do you know if the integration tests should use the same logger as C2R?

@Venefilyn
Copy link
Member Author

Looks like sanity checks are failing as the test doesn't recognise the convert2rhel module importing root_logger in tests/integration/conftest.py. Do you know if the integration tests should use the same logger as C2R?

@SerCantus good spot. I don't think I should change the integration tests. I changed it back now and will re-trigger tests

Comment on lines 295 to 300
new_fmt = fmt_orig if not color or self.color_disabled else colorize(fmt_orig, color)
self._fmt = new_fmt
Copy link
Member

Choose a reason for hiding this comment

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

What do you think unpacking the if-else one-liner to this?

In my opinion, it makes easier to read when we have a if with more than one statement like the current one.

Suggested change
new_fmt = fmt_orig if not color or self.color_disabled else colorize(fmt_orig, color)
self._fmt = new_fmt
new_fmt = fmt_orig
if color or not self.color_disaled:
new_fmt = colorize(fmt_orig, color)
self._fmt = new_fmt

Not a required change, to be honest, just posting in case if you agree to apply.

Comment on lines 314 to 334
class CustomLogger(logging.Logger):
def __init__(self, name, level=0):
Copy link
Member

Choose a reason for hiding this comment

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

Can we have a docstring here mentioning why this class exist? Looking at it and understanding the context of the PR is easy to guess, but future people might not get it at first.

Copy link
Member

Choose a reason for hiding this comment

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

Especially if you can include that the critical_no_exit, task and file were moved here to avoid messing up with the parent logger class

@r0x0d
Copy link
Member

r0x0d commented Sep 11, 2024

/packit test --labels sanity

This refactor of the usage of our logging is to get away from custom
functions that causes attribute errors in editors and IDEs. This will
also standarize the usage of logger and the loglevels.

In future PRs we can include changes to include prepare, rollback, and
final specific message styles.
@Venefilyn
Copy link
Member Author

@r0x0d Addressed your comments, WDYT?

@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@r0x0d r0x0d merged commit 6768a26 into oamg:main Sep 11, 2024
27 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip/changelog If it should be excluded from changelog or Release notes. Such as infra, reverted PRs, etc. tests/sanity PR ready to run the sanity test suit. Equivalent to `/packit test --labels sanity`.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants