Skip to content

Commit a77b392

Browse files
author
Jacques Troussard
committed
532: moves the logger warning to the requests initializer, updates tests
1 parent 103d28c commit a77b392

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

requests_oauthlib/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@
2121

2222
logging.getLogger("requests_oauthlib").addHandler(logging.NullHandler())
2323
logging.getLogger("requests_oauthlib").addFilter(DebugModeTokenFilter())
24+
25+
for filter_ in logging.getLogger("requests_oauthlib").filters:
26+
if isinstance(filter_, DebugModeTokenFilter):
27+
if filter_.mode == 'DEFAULT':
28+
msg = "Your logger, when in DEBUG mode, will log TOKENS"
29+
logging.warning(msg)
30+
break

requests_oauthlib/log_filters.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self):
2020
environment variable.
2121
"""
2222
super().__init__()
23-
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()
23+
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()
2424

2525
def filter(self, record):
2626
"""
@@ -37,7 +37,4 @@ def filter(self, record):
3737
record.msg = re.sub(r'Bearer (\w+)', '[MASKED]', record.getMessage())
3838
elif self.mode == "SUPPRESS":
3939
return False
40-
elif self.mode == "DEFAULT":
41-
msg = "Your logger, when in DEBUG mode, will log TOKENS"
42-
raise Warning(msg)
43-
return True
40+
return True # if mode is not MASKED then DEFAULT is implied

tests/test_log_filters.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ def test_suppress_mode(self):
2323
@patch.dict('os.environ', {'DEBUG_MODE_TOKEN_FILTER': 'DEFAULT'})
2424
def test_default_mode_raises_warning(self):
2525
filter = DebugModeTokenFilter()
26-
with self.assertRaises(Warning) as context:
27-
filter.filter(self.record)
28-
self.assertTrue("Your logger, when in DEBUG mode, will log TOKENS" in str(context.exception))
26+
result = filter.filter(self.record)
27+
self.assertTrue(result)
2928

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

0 commit comments

Comments
 (0)