I know this doesn't help for Python, but... You can do that in Java with instrumentation. Things like ByteBuddy [1] or ReactorDebugAgent [2] will add debugging statements into your code without you having to touch anything.
If you want more advanced, then you can look at libstapsdt [3] which enables JVM applications to create USDT probes.
[1] https://tersesystems.github.io/terse-logback/guide/instrumen...
[2] https://github.com/reactor/reactor-tools#reactor-debug-agent...