diff --git a/python/nav/util.py b/python/nav/util.py index c7b609d309..4baf8cf493 100644 --- a/python/nav/util.py +++ b/python/nav/util.py @@ -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 + return False diff --git a/python/nav/web/auth/remote_user.py b/python/nav/web/auth/remote_user.py index fe853503da..da03807da8 100644 --- a/python/nav/web/auth/remote_user.py +++ b/python/nav/web/auth/remote_user.py @@ -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: @@ -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) workaround = 'none' try: workaround_config = _config.get('remote-user', 'workaround')