raven-python icon indicating copy to clipboard operation
raven-python copied to clipboard

Configure logging of user info in Django

Open aej opened this issue 7 years ago • 4 comments

With GDPR coming up soon it would be really useful to be able to configure the type of user info we log in the Django client. Ideally we should be able to disable logging ip address, username and email via a setting.

aej avatar May 02 '18 16:05 aej

Yes! Seems like this has been hardcoded in https://github.com/getsentry/raven-python/blob/master/raven/contrib/django/client.py#L158-L186, so guess the way to go is to override this method? But how do I use a modified version of the DjangoClient class?

Update: Seems like one can set the SENTRY_CLIENT setting. https://github.com/getsentry/raven-python/blob/master/raven/contrib/django/models.py#L115 will try and report back :)

valberg avatar May 14 '18 14:05 valberg

Yes, that worked!

Here is the new client class:

from raven.contrib.django.client import DjangoClient


class GDPRCompliantDjangoClient(DjangoClient):

    def get_user_info(self, request):
        user_info = {}
        user = getattr(request, 'user', None)

        if user is None:
            return user_info

        authenticated = user.is_authenticated

        if not authenticated:
            return user_info
        user_info['id'] = user.pk
        user_info['username'] = user.username

        return user_info

and I've just set the following setting:

SENTRY_CLIENT = 'path.to.GDPRCompliantDjangoClient'

valberg avatar May 14 '18 14:05 valberg

Nice stuff! I didnt realise you could configure the client in that way. I think this is an acceptable solution.

aej avatar May 18 '18 09:05 aej

Will be changing how some of the stuff is handled by default soon, but yes, you can configure your own django client.

ashwoods avatar May 18 '18 09:05 ashwoods