-
Notifications
You must be signed in to change notification settings - Fork 45
Make it possible to skip logging before the actual call #2745
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -535,3 +535,18 @@ def resource_bytes(package, filename): | |
| """ | ||
| ref = resource_files(package) / filename | ||
| return ref.read_bytes() | ||
|
|
||
|
|
||
| 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 | ||
|
Comment on lines
+540
to
+552
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,10 +20,13 @@ | |||||||||
| from os.path import join | ||||||||||
| import secrets | ||||||||||
|
|
||||||||||
| from django.conf import settings | ||||||||||
|
|
||||||||||
| from nav.auditlog.models import LogEntry | ||||||||||
| from nav.config import NAVConfigParser | ||||||||||
| from nav.models.profiles import Account | ||||||||||
| from nav.web.auth.utils import set_account | ||||||||||
| from nav.util import check_log_level | ||||||||||
|
|
||||||||||
|
|
||||||||||
| __all__ = [] | ||||||||||
|
|
@@ -178,6 +181,10 @@ def get_username(request): | |||||||||
| if not request: | ||||||||||
| return None | ||||||||||
|
|
||||||||||
| if settings.DEBUG or check_log_level(_logger, logging.DEBUG): | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||
| for metakey, value in request.META.items(): | ||||||||||
| if metakey[0] == metakey[0].upper(): | ||||||||||
| _logger.debug('%s: %s', metakey, value) | ||||||||||
| workaround = 'none' | ||||||||||
| try: | ||||||||||
| workaround_config = _config.get('remote-user', 'workaround') | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import logging | ||
|
|
||
| import pytest | ||
|
|
||
| from nav.util import check_log_level | ||
|
|
||
|
|
||
| class TestCheckLogLevel: | ||
| def test_actual_loglevel_above_input_should_return_false(self, *_): | ||
| logger = logging.getLogger() | ||
| logger.setLevel(logging.INFO) | ||
| assert check_log_level(logger, logging.DEBUG) is False | ||
|
|
||
| def test_actual_loglevel_below_input_should_return_true(self, *_): | ||
| logger = logging.getLogger() | ||
| logger.setLevel(logging.INFO) | ||
| assert check_log_level(logger, logging.WARNING) |
Uh oh!
There was an error while loading. Please reload this page.