From 68235f75523db96e5f5680557954e507491d38d5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Thu, 9 Feb 2012 02:11:59 -0200 Subject: [PATCH] Improve logging. Closes #244 --- social_auth/backends/__init__.py | 9 +++------ social_auth/backends/contrib/foursquare.py | 2 -- social_auth/backends/contrib/github.py | 3 --- social_auth/backends/contrib/linkedin.py | 3 --- social_auth/backends/contrib/livejournal.py | 2 -- social_auth/backends/contrib/orkut.py | 2 -- social_auth/backends/facebook.py | 17 +++++++---------- social_auth/backends/google.py | 3 --- social_auth/backends/twitter.py | 3 --- social_auth/backends/yahoo.py | 3 --- social_auth/utils.py | 16 ++++++++++++++++ social_auth/views.py | 11 ++++------- 12 files changed, 30 insertions(+), 44 deletions(-) diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index f01ceba..4a946d2 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -9,9 +9,6 @@ Also the modules *must* define a BACKENDS dictionary with the backend name (which is used for URLs matching) and Auth class, otherwise it won't be enabled. """ -import logging -logger = logging.getLogger(__name__) - from urllib2 import Request, urlopen from urllib import urlencode from urlparse import urlsplit @@ -29,7 +26,7 @@ from django.contrib.auth.backends import ModelBackend from django.utils import simplejson from django.utils.importlib import import_module -from social_auth.utils import setting +from social_auth.utils import setting, log from social_auth.store import DjangoOpenIDStore from social_auth.backends.exceptions import StopPipeline @@ -135,7 +132,7 @@ class SocialAuthBackend(ModelBackend): try: mod = import_module(mod_name) except ImportError: - logger.exception('Error importing pipeline %s', name) + log('exception', 'Error importing pipeline %s', name) else: func = getattr(mod, func_name, None) @@ -690,4 +687,4 @@ def get_backend(name, *args, **kwargs): BACKENDS = { 'openid': OpenIdAuth -} \ No newline at end of file +} diff --git a/social_auth/backends/contrib/foursquare.py b/social_auth/backends/contrib/foursquare.py index 9e70d9e..0ba718a 100644 --- a/social_auth/backends/contrib/foursquare.py +++ b/social_auth/backends/contrib/foursquare.py @@ -1,6 +1,4 @@ import urllib -import logging -logger = logging.getLogger(__name__) from django.utils import simplejson diff --git a/social_auth/backends/contrib/github.py b/social_auth/backends/contrib/github.py index 11df0e0..b866ca3 100644 --- a/social_auth/backends/contrib/github.py +++ b/social_auth/backends/contrib/github.py @@ -13,9 +13,6 @@ field, check OAuthBackend class for details on how to extend it. """ import cgi import urllib -import logging -logger = logging.getLogger(__name__) - from django.utils import simplejson from django.contrib.auth import authenticate diff --git a/social_auth/backends/contrib/linkedin.py b/social_auth/backends/contrib/linkedin.py index 94fae9f..ae92716 100644 --- a/social_auth/backends/contrib/linkedin.py +++ b/social_auth/backends/contrib/linkedin.py @@ -3,9 +3,6 @@ Linkedin OAuth support No extra configurations are needed to make this work. """ -import logging -logger = logging.getLogger(__name__) - from xml.etree import ElementTree from xml.parsers.expat import ExpatError diff --git a/social_auth/backends/contrib/livejournal.py b/social_auth/backends/contrib/livejournal.py index 2e516a0..ce9e538 100644 --- a/social_auth/backends/contrib/livejournal.py +++ b/social_auth/backends/contrib/livejournal.py @@ -5,8 +5,6 @@ This contribution adds support for LiveJournal OpenID service in the form username.livejournal.com. Username is retrieved from the identity url. """ import urlparse -import logging -logger = logging.getLogger(__name__) from social_auth.backends import OpenIDBackend, OpenIdAuth, USERNAME diff --git a/social_auth/backends/contrib/orkut.py b/social_auth/backends/contrib/orkut.py index 7ad4054..c7ed4d2 100644 --- a/social_auth/backends/contrib/orkut.py +++ b/social_auth/backends/contrib/orkut.py @@ -11,8 +11,6 @@ OAuth settings ORKUT_CONSUMER_KEY and ORKUT_CONSUMER_SECRET are needed to enable this service support. """ import urllib -import logging -logger = logging.getLogger(__name__) from django.utils import simplejson diff --git a/social_auth/backends/facebook.py b/social_auth/backends/facebook.py index 57e30cf..5fe4d36 100644 --- a/social_auth/backends/facebook.py +++ b/social_auth/backends/facebook.py @@ -11,10 +11,6 @@ setting, it must be a list of values to request. By default account id and token expiration time are stored in extra_data field, check OAuthBackend class for details on how to extend it. """ -import logging -logger = logging.getLogger(__name__) - - import cgi from urllib import urlencode from urllib2 import urlopen @@ -23,7 +19,7 @@ from django.utils import simplejson from django.contrib.auth import authenticate from social_auth.backends import BaseOAuth2, OAuthBackend, USERNAME -from social_auth.utils import sanitize_log_data, setting +from social_auth.utils import sanitize_log_data, setting, log # Facebook configuration @@ -67,13 +63,14 @@ class FacebookAuth(BaseOAuth2): try: data = simplejson.load(urlopen(url)) - logger.debug('Found user data for token %s', - sanitize_log_data(access_token), - extra=dict(data=data)) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} - logger.error('Could not load user data from Facebook.', - exc_info=True, extra=extra) + log('error', 'Could not load user data from Facebook.', + exc_info=True, extra=extra) + else: + log('debug', 'Found user data for token %s', + sanitize_log_data(access_token), + extra=dict(data=data)) return data def auth_complete(self, *args, **kwargs): diff --git a/social_auth/backends/google.py b/social_auth/backends/google.py index 6a94cc0..88896ab 100644 --- a/social_auth/backends/google.py +++ b/social_auth/backends/google.py @@ -13,9 +13,6 @@ APIs console https://code.google.com/apis/console/ Identity option. OpenID also works straightforward, it doesn't need further configurations. """ -import logging -logger = logging.getLogger(__name__) - from urllib import urlencode from urllib2 import Request, urlopen diff --git a/social_auth/backends/twitter.py b/social_auth/backends/twitter.py index 759fe79..f71f349 100644 --- a/social_auth/backends/twitter.py +++ b/social_auth/backends/twitter.py @@ -11,9 +11,6 @@ User screen name is used to generate username. By default account id is stored in extra_data field, check OAuthBackend class for details on how to extend it. """ -import logging -logger = logging.getLogger(__name__) - from django.utils import simplejson from social_auth.backends import ConsumerBasedOAuth, OAuthBackend, USERNAME diff --git a/social_auth/backends/yahoo.py b/social_auth/backends/yahoo.py index e17577e..e9362f4 100644 --- a/social_auth/backends/yahoo.py +++ b/social_auth/backends/yahoo.py @@ -3,9 +3,6 @@ Yahoo OpenID support No extra configurations are needed to make this work. """ -import logging -logger = logging.getLogger(__name__) - from social_auth.backends import OpenIDBackend, OpenIdAuth diff --git a/social_auth/utils.py b/social_auth/utils.py index ec5c09e..4033867 100644 --- a/social_auth/utils.py +++ b/social_auth/utils.py @@ -1,4 +1,5 @@ import urlparse +import logging from collections import defaultdict from django.conf import settings @@ -85,6 +86,21 @@ def setting(name, default=None): return getattr(settings, name, default) +logger = None +if not logger: + logging.basicConfig() + logger = logging.getLogger('SocialAuth') + logger.setLevel(logging.DEBUG) + + +def log(level, *args, **kwargs): + """Small wrapper around logger functions.""" + { 'debug': logger.debug, + 'error': logger.error, + 'exception': logger.exception, + 'warn': logger.warn }[level](*args, **kwargs) + + if __name__ == '__main__': import doctest doctest.testmod() diff --git a/social_auth/views.py b/social_auth/views.py index 1dc36f0..29774a4 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -5,9 +5,6 @@ Notes: on third party providers that (if using POST) won't be sending crfs token back. """ -import logging -logger = logging.getLogger(__name__) - from functools import wraps from django.http import HttpResponseRedirect, HttpResponse, \ @@ -19,7 +16,7 @@ from django.contrib import messages from django.views.decorators.csrf import csrf_exempt from social_auth.backends import get_backend -from social_auth.utils import sanitize_redirect, setting +from social_auth.utils import sanitize_redirect, setting, log DEFAULT_REDIRECT = setting('SOCIAL_AUTH_LOGIN_REDIRECT_URL') or \ @@ -53,14 +50,14 @@ def dsa_view(redirect_name=None): raise backend_name = backend.AUTH_BACKEND.name - logger.error(unicode(e), exc_info=True, - extra=dict(request=request)) + log('error', unicode(e), exc_info=True, + extra=dict(request=request)) if 'django.contrib.messages' in setting('INSTALLED_APPS'): from django.contrib.messages.api import error error(request, unicode(e), extra_tags=backend_name) else: - logger.warn('Messages framework not in place, some '+ + log('warn', 'Messages framework not in place, some '+ 'errors have not been shown to the user.') url = setting('SOCIAL_AUTH_BACKEND_ERROR_URL', LOGIN_ERROR_URL) -- 2.39.5