I'm looking at optimising some code.
For example if I have set Python's log level to info and I write code e.g.
logger.debug(....)
Does Python know that I am set to info level and effectively throw the debug statement away?
Is there a way to determine which log level is set and I could test this before doing more costly operations such as formatting strings for logging? Is it better to do this or just log and let Python determine what should be logged.
Which approach is better for performance?
Does Python know that I am set to
infolevel and effectively throw the debug statement away?
First of all no Python statement ever disappears. Python is not like C that will just remove unused code. For Python there is no such thing as unused code (unfortunately). The call will always happen, formatting will always happen. So the only question is: is that really a performance issue and what logic happens underneath the call?
First let me address the .debug() call itself. Python doesn't know much on its own. It totally depends on logger implementation. By default Python loggers won't do anything when they are not enabled for a given level. However this behaviour can be overridden. And this is not some abstract, academic situation, third-party libraries do provide their own implementations all the time. On the other hand such behaviour (i.e. do something even though not allowed because of level) would be a very nasty and unfriendly one. And you can assume (as most of us do) that no such thing happens unless you have a reason to doubt it.
Which approach is better for performance?
Now for the string formatting. If you worry about it then IMO you are wasting time. This is a micro optimization and if you need this level of performance then most likely you should not be using Python to begin with.
The only reason logging could be problematic is if it is logging over network or possibly file system.
Either way: measure first, don't speculate.
That being said, if you really are worried about the performance then you can always utilize what these docs talk about:
if logger.isEnabledFor(logging.DEBUG):
logger.debug('Message with %s, %s', expensive_func1(),
expensive_func2())
But please do read docs and familiarize with potential issues with .isEnabledFor() call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With