I do this with lots of old code or when I’m starting a not very important project, it is really quite simple as long as you don’t mind your debugger yelling at you.
What I do (in this order in the script) is make a deepcopy of print
, (print2 or whatever), then build a wrapper function that does logging code, then the copy of print.
Then you replace the builtin print with your wrapper function.
from copy import deepcopy
print2 = deepcopy(print)
def print_wrapper(*args, **kwargs):
#do some logging code
#do some anvil logging code
example_send_to_anvil_function(*args, **kwargs)
print2(*args, **kwargs)
print = print_wrapper
This is definitely on the “please don’t do this” list of things people do anyway.