【发布时间】:2016-10-14 14:10:43
【问题描述】:
我想扩展现有的logging.LEVEL 机制,以便我可以选择在不同的日志记录级别之间切换,例如DEBUG、INFO、ERROR 等,但还可以为每个级别定义一个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()以获取不同级别的详细信息,这可能是一个可能的解决方案。但我想以更优雅的方式解决这个问题。