Skip to content

Commit

Permalink
Fix issue #665: Add method to get / set debug flag (#668)Merge pull r…
Browse files Browse the repository at this point in the history
…equest #668 from Abhishek8394/issue-665

Fix issue #665: Add method to get / set debug flag
  • Loading branch information
JonathanHuot committed Apr 24, 2019
2 parents 81a295d + 924a58f commit 30321dd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/error_reporting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ case where that is not true please let us know!

When reporting bugs, especially when they are hard or impossible to reproduce,
it is useful to include logging output. You can enable logging for all
oauthlib modules by adding a logger to the `oauthlib` namespace.
oauthlib modules by adding a logger to the `oauthlib` namespace. You might also
want to enable debugging mode to include request data in output.

.. code-block:: python
import logging
import oauthlib
import sys
oauthlib.set_debug(True)
log = logging.getLogger('oauthlib')
log.addHandler(logging.StreamHandler(sys.stdout))
log.setLevel(logging.DEBUG)
If you are using a library that builds upon OAuthLib please also enable the
logging for their modules, e.g. for `requests-oauthlib`

Expand Down
2 changes: 2 additions & 0 deletions docs/oauth1/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ Drop a line in our `Gitter OAuthLib community`_ or open a `GitHub issue`_ =)
If you run into issues it can be helpful to enable debug logging::

import logging
import oauthlib
import sys
oauthlib.set_debug(True)
log = logging.getLogger('oauthlib')
log.addHandler(logging.StreamHandler(sys.stdout))
log.setLevel(logging.DEBUG)
3 changes: 3 additions & 0 deletions docs/oauth2/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ If you run into issues it can be helpful to enable debug logging.
.. code-block:: python
import logging
import oauthlib
import sys
oauthlib.set_debug(True)
log = logging.getLogger('oauthlib')
log.addHandler(logging.StreamHandler(sys.stdout))
log.setLevel(logging.DEBUG)
17 changes: 17 additions & 0 deletions oauthlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@
__version__ = '3.0.2-dev'

logging.getLogger('oauthlib').addHandler(NullHandler())

_DEBUG = False

def set_debug(debug_val):
"""Set value of debug flag
:param debug_val: Value to set. Must be a bool value.
"""
global _DEBUG
_DEBUG = debug_val

def get_debug():
"""Get debug mode value.
:return: `True` if debug mode is on, `False` otherwise
"""
return _DEBUG
3 changes: 3 additions & 0 deletions oauthlib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re
import sys
import time
from . import get_debug

try:
from secrets import randbits
Expand Down Expand Up @@ -435,6 +436,8 @@ def __getattr__(self, name):
raise AttributeError(name)

def __repr__(self):
if not get_debug():
return "<oauthlib.Request SANITIZED>"
body = self.body
headers = self.headers.copy()
if body:
Expand Down
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import oauthlib

oauthlib.set_debug(True)
16 changes: 16 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import os
import sys

import oauthlib
from oauthlib.common import (CaseInsensitiveDict, Request, add_params_to_uri,
extract_params, generate_client_id,
generate_nonce, generate_timestamp,
Expand Down Expand Up @@ -214,6 +216,20 @@ def test_headers_params(self):
self.assertEqual(r.headers['token'], 'foobar')
self.assertEqual(r.token, 'banana')

def test_sanitized_request_non_debug_mode(self):
"""make sure requests are sanitized when in non debug mode.
For the debug mode, the other tests checking sanitization should prove
that debug mode is working.
"""
try:
oauthlib.set_debug(False)
r = Request(URI, headers={'token': 'foobar'}, body='token=banana')
self.assertNotIn('token', repr(r))
self.assertIn('SANITIZED', repr(r))
finally:
# set flag back for other tests
oauthlib.set_debug(True)


class CaseInsensitiveDictTest(TestCase):

Expand Down

0 comments on commit 30321dd

Please sign in to comment.