-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_log.py
35 lines (25 loc) · 948 Bytes
/
user_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import logging
from django.contrib.auth.signals import (
user_logged_in,
user_logged_out,
user_login_failed,
)
from django.dispatch import receiver
log = logging.getLogger(__name__)
@receiver(user_logged_in)
def user_logged_in_callback(sender, request, user, **kwargs):
# to cover more complex cases:
# http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
ip = request.META.get("REMOTE_ADDR")
log.debug("login user: {user} via ip: {ip}".format(user=user, ip=ip))
@receiver(user_logged_out)
def user_logged_out_callback(sender, request, user, **kwargs):
ip = request.META.get("REMOTE_ADDR")
log.debug("logout user: {user} via ip: {ip}".format(user=user, ip=ip))
@receiver(user_login_failed)
def user_login_failed_callback(sender, credentials, **kwargs):
log.warning(
"login failed for: {credentials}".format(
credentials=credentials,
)
)