Skip to content

Commit

Permalink
Make it possible to skip logging before the actual call
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Nov 16, 2023
1 parent bb794f2 commit c2d8343
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/nav/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,20 @@ def _range_to_str(x, y):
return str(x)
else:
return "{}-{}".format(x, y)


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()
if level <= loglevel:
return True

Check warning on line 527 in python/nav/util.py

View check run for this annotation

Codecov / codecov/patch

python/nav/util.py#L527

Added line #L527 was not covered by tests
return False
7 changes: 7 additions & 0 deletions python/nav/web/auth/remote_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import logging
from os.path import join

from django.conf import settings

from nav.auditlog.models import LogEntry
from nav.config import NAVConfigParser
from nav.models.profiles import Account
from nav.util import check_log_level
from nav.web.auth.utils import ACCOUNT_ID_VAR

try:
Expand Down Expand Up @@ -179,6 +182,10 @@ def get_username(request):
if not request:
return None

if settings.DEBUG or check_log_level(_logger, logging.DEBUG):
for metakey, value in request.META.items():
if metakey[0] == metakey[0].upper():
_logger.debug('%s: %s', metakey, value)

Check warning on line 188 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L186-L188

Added lines #L186 - L188 were not covered by tests
workaround = 'none'
try:
workaround_config = _config.get('remote-user', 'workaround')
Expand Down

0 comments on commit c2d8343

Please sign in to comment.