Which is the best way to log user actions in flask views?

I would like to log user actions whenever user logs in/out and adds, edits, deletes objects in my site models in flask. Which is the best way to do this? Also I would like to show the old data and the new modified data, which happens using wtfforms. I am using flask and Flask-SQLAlchemy. I want something similar to what Django framework offers in the ‘History‘ hlink for the associated objects.

Best answer

Use Signals. Take a look at this

http://flask.pocoo.org/docs/signals/

Using signals, you can keep track of any actions such as adds/edits etc. as needed. All you have to do is

from blinker import Namespace
my_signals = Namespace()

def add_user():
    # add user code here
    user_added = my_signals.signal('user-added')