简体   繁体   English

python - 哪种启用/禁用日志记录的更好方法?

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

Which is better way to enable/disable logging? 哪种方法可以更好地启用/禁用日志记录?

1) Changing log levels, 1)更改日志级别,

logging.disable(logging.CRITICAL)

2) 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... 这样我们可以在禁用日志记录的情况下避免不必要的字符串构造...

1 is best, ideally via a configuration file or command line argument (--quiet) 1是最好的,理想情况下通过配置文件或命令行参数(--quiet)

2 will just clutter up your code 2只会使你的代码混乱

If you want to avoid expensive string construction (this is probably worthwhile about 0.001% of the time in my experience), use: 如果你想避免昂贵的字符串构造(根据我的经验,这可能值0.001%的时间),使用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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