Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - Which is the better way to enable/disable logging?

Tags:

python

logging

Which is better way to enable/disable logging?

1) Changing log levels,

logging.disable(logging.CRITICAL)

2)

log = None

And logging messages this way,

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

So that we can avoid unnecessary string constructions in case of logging disabled...

like image 933
asdfg Avatar asked Dec 17 '22 00:12

asdfg


1 Answers

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

2 will just clutter up your code

If you want to avoid expensive string construction (this is probably worthwhile 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

like image 108
Peter Lyons Avatar answered Jan 10 '23 06:01

Peter Lyons