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.
Asked
Active
Viewed 206 times
0
-
3You might look into the [`sys.settrace`](https://docs.python.org/2/library/sys.html#sys.settrace) function to see if it meets your needs. – jedwards Jul 20 '16 at 05:39
-
1Any reason not to use the [`logging`](https://docs.python.org/3/library/logging.html) module? – Alex Jul 20 '16 at 05:39
2 Answers
1
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.

Alex
- 18,484
- 8
- 60
- 80
0
sys.settrace traces a function for debugging but can be modified for this problem. Maybe something like this -
import sys
def trace_calls(frame, event, arg):
if event != 'call':
return
co = frame.f_code
func_name = co.co_name
if func_name == 'write':
# Ignore write() calls from print statements
return
func_line_no = frame.f_lineno
func_filename = co.co_filename
caller = frame.f_back
caller_line_no = caller.f_lineno
caller_filename = caller.f_code.co_filename
print 'Call to %s on line %s of %s from line %s of %s' % \
(func_name, func_line_no, func_filename,
caller_line_no, caller_filename)
Also see profiling. It describes how often and for how long various parts of the program executed

Sahil Agarwal
- 555
- 3
- 12