I'm using standard python logging module in my python application:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger("log") while True: logger.debug('Stupid log message " + ' '.join([str(i) for i in range(20)]) ) # Do something
The issue is that although debug level is not enable, that stupid log message is evaluated on each loop iteration, which harms performance badly.
Is there any solution for this?
In C++ we have log4cxx
package that provides macros like this:
LOG4CXX_DEBUG(logger, messasage)
That effectively evaluates to
if (log4cxx::debugEnabled(logger)) { log4cxx.log(logger,log4cxx::LOG4CXX_DEBUG, message) }
But since there are no macros in Python (AFAIK), if there a efficient way to do logging?
Best Answer
The logging module already has partial support for what you want to do. Do this:
... instead of this:
The logging module is smart enough to not produce the complete log message unless the message actually gets logged somewhere.
To apply this feature to your specific request, you could create a lazyjoin class.
Use it like this (note the use of a generator expression, adding to the laziness):
Here is a demo that shows this works.
In the demo, The logger.info() call hit the assertion error, while logger.debug() did not get that far.