I am developing a project in which I have to store all the function that were called in each request-response cycle and store them. I do not need to store values of the variable, all I need to store is function that we were called with their parameters and their order in execution. I am using mongodb to store this trace.
You could use a function decorator for convenience.
import functools
import logging
def log_me(func):
@functools.wraps(func)
def inner(*args, **kwargs):
logging.debug('name: %s, args: %s, kwargs: %s', func.__name__, args, kwargs)
return func(*args, **kwargs)
return inner
Then decorate your function(s) to log.
@log_me
def test(x):
return x + 2
Test call.
In [10]: test(3)
DEBUG:root:name: test, args: (3,), kwargs: {}
Out[10]: 5
If you wanted to store the entries in MongoDB directly instead of first logging to the logging
module you can replace the logging.debug
line with code that creates an entry in your database.