Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging events in django application

Tags:

python

django

I need to log some business events in my Django 1.4 application. For example, when user send a refferal email, or user makes a purchase. And so on. This event log will be used for some busness analytics. Is there any best practisies for doing this in e-commercial applications? May be a service or django module? I've never developed such systems before and I need some advice.

like image 835
Paul Avatar asked May 28 '14 03:05

Paul


1 Answers

I know you're talking about using the built-in but just for give another option, I usually use a model to store log action in some e-commerce pages, such as:

class MyLog(models.Model):
    LOG_ACTIONS = (
        ('login', 'User logged in'),
        ('delete_object', 'User delete object'),
        ('create_object', 'User create object'),
        ('buy_object', 'User buy object'),
        #   ...     ...     ...
        # Whatever you need here
    )
    user = models.ForeignKey(User)
    action = models.CharField(max_length=20, default='login', choices=LOG_ACTIONS, verbose_name= 'action')
    date = models.DateTimeField(auto_now_add=True, verbose_name='date')

    class Meta:
        ordering = ['-dt']
        verbose_name = 'Action log'
        verbose_name_plural = 'Actions log'

The example is simplified, you can use as many fields as you need and as many log actions as you need.

So when you do any action that have to be in the log, just create an object for MyLog, and when you want to get all log actions, just get MyLog objects, and you can filter them by date, user or any field you put in there.

Hope it helps!

like image 98
AlvaroAV Avatar answered Sep 23 '22 22:09

AlvaroAV