【问题标题】:Python Log Level with an additional verbosity depth具有额外详细深度的 Python 日志级别
【发布时间】:2016-10-14 14:10:43
【问题描述】:

我想扩展现有的logging.LEVEL 机制,以便我可以选择在不同的日志记录级别之间切换,例如DEBUGINFOERROR 等,但还可以为每个级别定义一个depth的级别。

例如,假设日志记录级别设置为logging.DEBUG。所有log.DEBUG() 调用都将可见。

log.debug('Limit event has occurred.')

所以我得到:

[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.

我所追求的是向log.debug() 调用传递一个额外的深度级别,以便我可以控制在DEBUG 消息中打印多少细节,而不是完全启用 或禁用 DEBUG 级别,但控制 debug 消息将携带多少信息。因此,在所有情况下,我们都会看到调试消息,但在某些情况下,它不太详细,并且在某些情况下包含更多信息。

例如:

log.debug('Limit event has occurred.', verbosity=1)
log.debug('The following user has caused the limit event: %s' % (user), verbosity=3)
log.debug('The following files have been affected: %s' % [list_of_files], verbosity=7)

所以当日志级别设置为DEBUG 并且全局详细程度设置为GLOBAL_VERBOSITY=1 时,我们将得到:

[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.

如果全局详细程度设置为GLOBAL_VERBOSITY=4,我们将得到:

[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.
[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) The following user has caused the limit event: xpr

如果全局详细程度设置为GLOBAL_VERBOSITY=9,我们将获得所有详细信息:

[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.
[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) The following user has caused the limit event: xpr
[2016-10-08 10:07:29,807] <__main__> {myApp:test_condition_info:93} (DEBUG) The following files have been affected: ['inside.ini', 'render.so']

我应该如何解决这个问题?

【问题讨论】:

  • 现在看我的问题,感觉就像拥有一个值为10 的主调试级别,它将用作log.debug(),然后拥有一组具有值的自定义调试级别11-19 称为 log.debugA()log.debugI() 以获取不同级别的详细信息,这可能是一个可能的解决方案。但我想以更优雅的方式解决这个问题。

标签: python logging


【解决方案1】:

您不能只使用更细粒度的日志记录级别吗? DEBUG 只是级别 10 的包装器。您可以使用

Logger.log(10, "message")

在调试级别登录,然后

Logger.log(9, "message")

它不会在调试级别显示,但如果你这样做会显示

Logger.setLevel(9)

如果你一心想换一种方式,你应该看看“过滤器”。

logging with filters

#!/usr/bin/env python
import logging

GLOBAL_VERBOSITY = 1

class LoggingErrorFilter(logging.Filter):
  def filter(self, record):
    if record.__dict__.get("verbosity", 0) > GLOBAL_VERBOSITY:
      print "Log message verbosity is greater than threshold, logging line:{0}".format(record)
      return True
    print "Log message verbosity is lower than threshold, not logging line:{0}".format(record)
    return False

logging.basicConfig(level=logging.DEBUG, filename="test.log")

logger = logging.getLogger()

filter = LoggingErrorFilter()

logger.addFilter(filter)


def main():
    logger.info("Message 1", extra={"verbosity":3})
    logger.info("Message 2", extra={"verbosity":1})


if __name__ == "__main__":
    main()

【讨论】:

  • 嗯,我认为,找到解决这个问题的方法将提供一种更优雅的设置方式。我真的需要解决这个问题:(
  • 使用“过滤器”添加/删除冗长?
  • 但是如何将verbosity=1 等额外参数传递给过滤器?过滤器似乎能够注入上下文信息,但所有示例都集中在动态生成的信息上:ipuser 等。没有示例演示使用消息本身。
  • 您可以将“额外”参数作为 **kwargs 传递 - 然后它们可以作为 LogRecord 字典的一部分使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多