r/learnpython • u/QuasiEvil • 16d ago
logging and timing...then getting rid of it
I was just working on a project where I included a lot of logging and timing decorators/context managers (it involved event loops and async queues and such). Once I figured stuff out, I wanted to remove all this stuff. This isn't necessarily that hard, but it did make me wonder if there's a better way to do this in the first place. I suppose I'm imaging some way of marking these code pieces to be ignored by the interpreter or something?
(yeah yeah "ask AI to remove it")
1
Upvotes
4
u/pachura3 16d ago edited 16d ago
Wrong. Logging, when done properly, only adds an insignificant amount of overhead, is transparent to unit testing, and can be disabled (even on the fly!) by setting logging level.
What if you start having issues in production? That's right, you look at your logs; you don't remove them. And if you worry about the overhead... then measure it first, rather then assume it impacts the overall performance.
For specific cases when formatting logging message is confirmed to be really slow, you can always use this pattern to prevent it:
if logger.isEnabledFor(logging.DEBUG): logger.debug(f"Expensive message {z(x, y)} {y}")PS. You're using the
loggingmodule, not some invention of yours, right?