You can insert calls to the following helper function at your points of interest (such as when the server is about to perform an action as a response) to dump into a log file the entire stack trace along with values of all local variables in each frame:
import sys
import traceback
def dump_stack(file=sys.stdout):
print(
*traceback.StackSummary.extract(
traceback.walk_stack(sys._getframe(1)), capture_locals=True
).format(), sep='\n', file=file
)
so that as an example:
def foo(b):
c = b + 1
dump_stack()
a = 1
foo(a)
would output something like:
File "./prog.py", line 13, in foo
b = 1
c = 2
File "./prog.py", line 16, in <module>
__annotations__ = {}
__builtins__ = <module 'builtins' (built-in)>
__cached__ = None
__doc__ = None
__file__ = '/home/bgjPHS/./prog'
__loader__ = <_frozen_importlib_external.SourcelessFileLoader object at 0x14ca185e5c10>
__name__ = '__main__'
__package__ = None
__spec__ = None
a = 1
dump_stack = <function dump_stack at 0x14ca185b2040>
foo = <function foo at 0x14ca183b7160>
sys = <module 'sys' (built-in)>
traceback = <module 'traceback' from '/usr/lib/python3.9/traceback.py'>
Demo: 4bgCb6 - Online Python3 Interpreter & Debugging Tool - Ideone.com