来自Java,建议使用
包装log.debugif (log.isDebugEnabled())
log.debug("blah blah blah "+someObject+" more blahs than you can blah at"):
在python中有这样的共鸣吗?或者python以不同的方式处理字符串?
答案 0 :(得分:2)
建议的答案忽略了字符串连接和任何繁重的方法调用的开销。
所以这个:
log.debug("blah blah blah "+someObject+" more blahs than you can blah at")
即使在ERROR日志记录级别,也会花费字符串联系。
不同的语言/记录器以不同的方式管理它,检查isDebugEnabled()或isEnabledFor()是很好的最佳实践。
当然,如果这不相关,你不应该预先优化,就像这个世界上的任何事情一样。
答案 1 :(得分:1)
无需额外检查。只需配置logging level:
即可>>> import logging
>>> root = logging.getLogger()
>>> root.setLevel(logging.INFO)
>>> root.addHandler(logging.StreamHandler())
>>> logging.error("test")
test
>>> logging.debug("test")
>>>
同样,不需要额外的检查(源代码取自logging/__init__.py
):
class Logger(Filterer):
...
def debug(self, msg, *args, **kwargs):
"""
Log 'msg % args' with severity 'DEBUG'.
To pass exception information, use the keyword argument exc_info with
a true value, e.g.
logger.debug("Houston, we have a %s", "thorny problem", exc_info=1)
"""
if self.isEnabledFor(DEBUG):
self._log(DEBUG, msg, args, **kwargs)
正如您所见,日志记录本身可以进行检查。
希望有所帮助。