简体   繁体   中英

Python: Perform operation dependent on logging level

One of my logger.debug() statements takes input that's fairly labour-intensive.

I know that I should do logger.debug("The spam is %s", spam_temperature) rather than logger.debug("The spam is {}".format(spam_temperature)) . The problem is that the operations I need to perform to actually find out the spam_temperature are quite CPU-intensive, and I have no use for them if the logging level is, say, INFO .

What is the best practice in a case like this?

Found the answer myself - I added python if not logging.getLogger().isEnabledFor(logging.DEBUG): return before the section I wanted to avoid.

(I got the inspiration from https://stackoverflow.com/a/27849836/3061818 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM