[Python-ideas] Non-intrusive debug logging
lsy at pobox.com
Wed Jan 24 18:25:09 EST 2018
Everyone uses logging during code development to help in debugging. Whether using a logging module or plain old print statements, this usually requires introducing one or (many) more lines of code into the model being worked on, making the existing, functional code more difficult to read. It is also easy to leave logging code in place accidentally when cleaning up. I have a possibly odd suggestion for dramatically improving debug logging, in an almost automated fashion, without any of the usual problems. I'm willing to jump through PEP hoops, but won't waste anyone's time arguing for the idea if others don't embrace it.
In brief, make an extremely succinct comment, such as "#l" or "#dbgl", semantically meaningful. If appended to a line of code, such as:
x = 1
Y = 2
Z = 3
x = y + z #l
When executed the line would be logged (presumably to stdout) as something like:
x=1 -> y=2 + z=3 -> 5
In one log line you see the variables and their values, the operations being performed with them, and the result of the evaluated expression.
Placing "#l" on a def Function() line would apply "#l" logging to every line in that function or method.
That's it. Adding these log requests would be easy and not clutter the code. Removing them reliably would be a trivial global operation in any text editor. The resulting debug information would be fully diagnostic.
Of course there are lots of details and edge cases to consider. There always are. For example, what if x had not been previously defined? A reasonable answer:
x=. -> y=2 + z=3 -> 5
In every case the solution should minimize volume of output while maximizing logged information.
One can debate the precise formatting of the output. The text used for the semantically meaningful comment string is open to debate. The output pipe could be made controllable. There are natural extensions to allow periodic and conditional logging. I have ideas for reducing (already unlikely) collisions with existing comments. But I want to keep this simple for now, and I think this captures the core of the idea.
For your consideration.
More information about the Python-ideas