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

Make it possible to skip logging before the actual call #2745

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hmpf
Copy link
Contributor

@hmpf hmpf commented Nov 16, 2023

Not particularly needed for where it is used in auth.remote_user but my sense of aesthetics was deeply offended by running a loop for no reason (if loglevel higher than DEBUG) and I wanted a general fix.

@hmpf hmpf self-assigned this Nov 16, 2023
Copy link

codecov bot commented Nov 16, 2023

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 56.70%. Comparing base (f92920c) to head (822ccb7).

Files Patch % Lines
python/nav/web/auth/remote_user.py 50.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2745   +/-   ##
=======================================
  Coverage   56.69%   56.70%           
=======================================
  Files         602      602           
  Lines       43971    43980    +9     
=======================================
+ Hits        24931    24937    +6     
- Misses      19040    19043    +3     

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

Copy link

github-actions bot commented Nov 16, 2023

Test results

     12 files       12 suites   11m 32s ⏱️
3 322 tests 3 322 ✔️ 0 💤 0
9 441 runs  9 441 ✔️ 0 💤 0

Results for commit 822ccb7.

♻️ This comment has been updated with latest results.

@hmpf hmpf force-pushed the skip-logging branch 2 times, most recently from 99827e4 to 28b1ee6 Compare November 21, 2023 07:53
Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

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

I'm not sure I get the full connection between the PR title and the actual content. It doesn't seem to be as much about adding generic functionality to skip logging as to actually adding some more debug logging to remote user processing

python/nav/util.py Outdated Show resolved Hide resolved
python/nav/util.py Show resolved Hide resolved
@@ -179,6 +182,10 @@ def get_username(request):
if not request:
return None

if settings.DEBUG or check_log_level(_logger, logging.DEBUG):
Copy link
Member

Choose a reason for hiding this comment

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

Is this extra function really necessary? I find the following to be just as clear:

Suggested change
if settings.DEBUG or check_log_level(_logger, logging.DEBUG):
if settings.DEBUG or _logger.getEffectiveLevel() <= logging.DEBUG:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function makes for slightly easier branch testing if one is fond of mocks and patches. Also it might prevent someone from prettifying getEffectiveLevel() to level or what it was, which does not do what one thinks it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

settings.DEBUG is probably not even necessary since that doesn't flip debug-logging magically on.

Copy link
Member

Choose a reason for hiding this comment

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

Well, this jogged my memory, and I recall NAV already has multiple examples of the (to me) even more legible pattern:

Suggested change
if settings.DEBUG or check_log_level(_logger, logging.DEBUG):
if settings.DEBUG or _logger.isEnabledFor(logging.DEBUG):

@hmpf
Copy link
Contributor Author

hmpf commented Nov 23, 2023

I'm not sure I get the full connection between the PR title and the actual content. It doesn't seem to be as much about adding generic functionality to skip logging as to actually adding some more debug logging to remote user processing

I needed more logging and also found a way to scratch a very scratchy itch that has bothered me for years. Maybe it should have been two PRs.

Copy link

sonarcloud bot commented Nov 23, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link

sonarcloud bot commented Jan 3, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@hmpf hmpf force-pushed the skip-logging branch 2 times, most recently from 66ba162 to 7e0c81a Compare February 27, 2024 06:33
Comment on lines +514 to +552
def check_log_level(logger, loglevel):
"""
Check that the level of the logger is equal to or below loglevel

Use before possibly expensive logging operations, like calling logging in
a loop or building an expensive object that will be logged and thrown away.

Avoid this classic mistake:

logger.info("%s", expensive_function())
"""
level = logger.getEffectiveLevel()
return level <= loglevel
Copy link
Member

Choose a reason for hiding this comment

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

Referring back to my previous review comments: Why do we need this at all? Why not just use logger.isEnabledFor(loglevel) ?

Copy link
Member

Choose a reason for hiding this comment

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

@hmpf hmpf force-pushed the skip-logging branch 6 times, most recently from 1ee3acc to 5edc150 Compare March 7, 2024 09:01
Copy link

sonarcloud bot commented Mar 7, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants