What is the best way to enable / disable logging?

What is the best way to enable / disable logging?

1) Change log levels,

logging.disable(logging.CRITICAL)

2)

log = None

And recording messages in this way

if log:
    log.info("log message")

To avoid unnecessary string constructs in the event of a log shutdown ...

+3
source share
1 answer

1 best, ideally, through a configuration file or command line argument (--quiet)

2 just clutter up your code

If you want to avoid the expensive string construction (this probably costs about 0.001% of the time in my experience), use:

if logger.isEnabledFor(logging.DEBUG):
    logger.debug("Message with %s, %s", expensive_func1(),
                                        expensive_func2())

http://docs.python.org/library/logging.html#optimization

+11
source

Source: https://habr.com/ru/post/1750797/


All Articles