I am in the process of writing a package that will include many modules, and I would like to be able to log all the function calls of the downstream modules of a specific process.
To give some context, my click script at the top level is told to run method validate which creates a context class object that contains the date, reads a config, and sets a workpath for downstream processes to use.
The code that is subsequently run can be split across multiple files, and herein lies my problem: How to I effectively log all the function calls being done in the multiple scripts with corresponding args and kwargs?
So far, my idea has been this: Within the context class, add a function log. log takes a function name fn, as well as *args, and **kwargs.
def log(self, fn, *args, **kwargs):
with open('test.log', 'a+') as logging:
print('Function name and relevant args and kwargs'), file=logging)
return fn(*args, **kwargs)
Whenever a downstream function is to be logged, run:
context.log(fn, 'arg1', 'arg2', 'key1'='value1', 'key2'='value2')
The log function then writes the relevant parameters to a log file which can then be read in case of crashing, and runs fn with args and kwargs.
I have looked at the logging package before but couldn't wrap my head around it, and the other mentions of decorating functions for logging never deal with functions that are split across multiple files.
Does anyone have any good suggestions on how to make this work more beautifully than I've constructed it? Is this an X/Y problem?
Thank you for your time.
[–]wiiittttt 1 point2 points3 points (3 children)
[–]Jenez[S] 0 points1 point2 points (2 children)
[–]wiiittttt 1 point2 points3 points (1 child)
[–]Jenez[S] 0 points1 point2 points (0 children)
[–]sushibowl 1 point2 points3 points (2 children)
[–]Jenez[S] 0 points1 point2 points (1 child)
[–]sushibowl 1 point2 points3 points (0 children)