Skip to content

Commit

Permalink
532: moves the logger warning to the requests initializer, updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques Troussard committed Mar 23, 2024
1 parent 103d28c commit a77b392
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
7 changes: 7 additions & 0 deletions requests_oauthlib/__init__.py
Expand Up @@ -21,3 +21,10 @@

logging.getLogger("requests_oauthlib").addHandler(logging.NullHandler())
logging.getLogger("requests_oauthlib").addFilter(DebugModeTokenFilter())

for filter_ in logging.getLogger("requests_oauthlib").filters:
if isinstance(filter_, DebugModeTokenFilter):
if filter_.mode == 'DEFAULT':
msg = "Your logger, when in DEBUG mode, will log TOKENS"
logging.warning(msg)
break
7 changes: 2 additions & 5 deletions requests_oauthlib/log_filters.py
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
environment variable.
"""
super().__init__()
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()

def filter(self, record):
"""
Expand All @@ -37,7 +37,4 @@ def filter(self, record):
record.msg = re.sub(r'Bearer (\w+)', '[MASKED]', record.getMessage())
elif self.mode == "SUPPRESS":
return False
elif self.mode == "DEFAULT":
msg = "Your logger, when in DEBUG mode, will log TOKENS"
raise Warning(msg)
return True
return True # if mode is not MASKED then DEFAULT is implied
5 changes: 2 additions & 3 deletions tests/test_log_filters.py
Expand Up @@ -23,9 +23,8 @@ def test_suppress_mode(self):
@patch.dict('os.environ', {'DEBUG_MODE_TOKEN_FILTER': 'DEFAULT'})
def test_default_mode_raises_warning(self):
filter = DebugModeTokenFilter()
with self.assertRaises(Warning) as context:
filter.filter(self.record)
self.assertTrue("Your logger, when in DEBUG mode, will log TOKENS" in str(context.exception))
result = filter.filter(self.record)
self.assertTrue(result)

if __name__ == '__main__':
unittest.main()

0 comments on commit a77b392

Please sign in to comment.