Skip to content

Commit c452480

Browse files
committed
Make it possible to skip logging before the actual call
1 parent bb794f2 commit c452480

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

python/nav/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,20 @@ def _range_to_str(x, y):
509509
return str(x)
510510
else:
511511
return "{}-{}".format(x, y)
512+
513+
514+
def check_log_level(logger, loglevel):
515+
"""
516+
Check that the level of the logger is equal to or below loglevel
517+
518+
Use before possibly expensive logging operations, like calling logging in
519+
a loop or building an expensive object that will be logged and thrown away.
520+
521+
Avoid this classic mistake:
522+
523+
logger.info("%s", expensive_function())
524+
"""
525+
level = logger.getEffectiveLevel()
526+
if level <= loglevel:
527+
return True
528+
return False

python/nav/web/auth/remote_user.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import logging
2020
from os.path import join
2121

22+
from django.conf import settings
23+
2224
from nav.auditlog.models import LogEntry
2325
from nav.config import NAVConfigParser
2426
from nav.models.profiles import Account
27+
from nav.util import check_log_level
2528
from nav.web.auth.utils import ACCOUNT_ID_VAR
2629

2730
try:
@@ -179,6 +182,10 @@ def get_username(request):
179182
if not request:
180183
return None
181184

185+
if settings.DEBUG or check_log_level(_logger, logging.DEBUG):
186+
for metakey, value in request.META.items():
187+
if metakey == metakey.upper():
188+
_logger.debug('%s: %s', metakey, value)
182189
workaround = 'none'
183190
try:
184191
workaround_config = _config.get('remote-user', 'workaround')

0 commit comments

Comments
 (0)