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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.