【问题标题】:Python: Perform operation dependent on logging levelPython:根据日志记录级别执行操作
【发布时间】:2016-04-29 08:10:51
【问题描述】:

我的一个logger.debug() 语句需要相当费力的输入。

我知道我应该做logger.debug("The spam is %s", spam_temperature) 而不是logger.debug("The spam is {}".format(spam_temperature))。问题是我需要执行以实际找出spam_temperature 的操作非常消耗CPU,如果日志级别是INFO,我对它们没有用处。

在这种情况下,最佳做法是什么?

【问题讨论】:

    标签: python logging


    【解决方案1】:

    自己找到了答案 - 我添加了 python if not logging.getLogger().isEnabledFor(logging.DEBUG): return 在我想避免的部分之前。

    (我的灵感来自https://stackoverflow.com/a/27849836/3061818

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-19
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      相关资源
      最近更新 更多