开发者

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

Which is better way to enable/disable logging?

1) Changing log levels,

logging.disable(logging.CRITICAL)

2)

log = No开发者_如何学运维ne

And logging messages this way,

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

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜