Commits

Alex Mayfield committed 81e4e08

Log user logins and IP addresses to logfile.

  • Participants
  • Parent commits cdb0d08

Comments (0)

Files changed (2)

File apps/profiles/models.py

+import logging
+
 from django.contrib.auth.models import User
+from django.contrib.auth.signals import user_logged_in
 from django.db import models
 from registration.signals import user_registered
 
         return self.user.username + "'s profile"
 
 # Signals
+def user_login(sender, request, user, **kwargs):
+    # Keep track of which IP address was used to login using a
+    # specific user.
+    logger = logging.getLogger('funcrusher.user_login')
+    logger.info('{username} ({ip}) has logged in'.format(
+        username=user.username,
+        ip=request.META['REMOTE_ADDR']
+    ))
+
 def user_created(sender, user, request, **kwargs):
+    # Grab profile information from registration form and save it with
+    # the new user.
     from forms import UserRegistrationForm
 
     form = UserRegistrationForm(request.POST)
     data.location = form.data['location']
     data.save()
 
+user_logged_in.connect(user_login)
 user_registered.connect(user_created)

File settings/settings.py

 LOGGING = {
     'version': 1,
     'disable_existing_loggers': False,
+    'formatters': {
+        'timestamped': {
+            'format': '[%(asctime)s] %(levelname)s "%(message)s"',
+        },
+    },
     'handlers': {
-        'mail_admins': {
-            'level': 'ERROR',
-            'class': 'django.utils.log.AdminEmailHandler'
-        }
+        'file': {
+            'class': 'logging.FileHandler',
+            'filename': SITE_ROOT + 'log/funcrusher.log',
+            'formatter': 'timestamped',
+        },
     },
     'loggers': {
-        'django.request': {
-            'handlers': ['mail_admins'],
-            'level': 'ERROR',
-            'propagate': True,
+        'funcrusher.user_login': {
+            'handlers': ['file'],
+            'level': 'INFO',
         },
-    }
+    },
 }
 
 AUTH_PROFILE_MODULE = 'profiles.profile'