日志采集

保存用户的登录日志该怎么做?

创建 用户登录日志数据库表

在 MVC 中创建相应的模型  

class LoginLog(models.Model):
    """登录日志"""
    # 日志ID
    logid = models.BigAutoField(primary_key=True)
    # 登录的用户
    user = models.ForeignKey(to=User, on_delete=models.DO_NOTHING, db_column='userid')
    # 登录的IP地址
    ipaddr = models.CharField(max_length=255)
    # 登录的日期
    logdate = models.DateTimeField(auto_now_add=True)
    # 登录的设备编码
    devcode = models.CharField(max_length=255, default='')

    class Meta:
        managed = False
        db_table = 'tb_login_log'

 

with atomic():
                current_time = timezone.now()
                if not user.lastvisit or \
                        (current_time - user.lastvisit).days >= 1:
                    user.point += 2
                    user.lastvisit = current_time
                    user.save()
                loginlog = LoginLog()
                loginlog.user = user
                loginlog.logdate = current_time
                loginlog.ipaddr = get_ip_address(request)
                loginlog.save()

 

posted @ 2020-06-06 17:56  扣剑书生  阅读(229)  评论(0)    收藏  举报