我的想法是创建上下文日志记录方案,如下例所示:[ DEBUG] Parsing dialogs files
[ DEBUG] ... [DialogGroup_001]
[ DEBUG] ...... Indexing dialog xml file [c:\001_dlg.xml]
[ DEBUG] ......... dialog [LobbyA]
[ DEBUG] ............ speech nodes [3]
[ DEBUG] ............... [LobbyA_01]
[ DEBUG] ............... [LobbyA_02]
[ DEBUG] ............... [LobbyA_03]
[ DEBUG] ............ sms nodes [0]
[ DEBUG] ......... dialog [LobbyB]
[ DEBUG] ............ speech nodes [3]
[ DEBUG] ............... [LobbyB_01]
[ DEBUG] ............... [LobbyB_02]
[ DEBUG] ............... [LobbyB_03]
[ DEBUG] ............ sms nodes [0]
[ DEBUG] ... [DialogGroup_002]
[ DEBUG] ...... Indexing dialog xml file [c:\002_dlg.xml]
[ DEBUG] ......... dialog [HighGroundsA]
[ DEBUG] ............ speech nodes [3]
[ DEBUG] ............... [HighGroundsA_01]
[ DEBUG] ............... [HighGroundsA_02]
[ DEBUG] ............... [HighGroundsA_03]
[ DEBUG] ............ sms nodes [0]
此时,我使用Python的日志模块,在日志记录时使用自定义的手写前缀,例如:(...)
log.debug('')
log.debug('Parsing dialogs files')
for dlg in defDlgList:
log.debug('... [{0}]'.format(dlg))
(...)
它工作得很好,但也有一些微妙的问题,例如:当从函数内部进行日志记录时,可以从不同的作用域调用它们,并且每个调用的前缀长度可能不同。
我正在寻找一种优雅而不可见的方法来为每个日志自动建立一个“…”前缀的长度。我宁愿避免将前缀长度作为参数传递给每个func或使用显式调用设置长度,例如:(...)
logWrapper.debug('')
logWrapper.debug('Parsing dialogs files')
for dlg in defDlgList:
logWrapper.nextLogLevelBegin()
logWrapper.debug('[{0}]'.format(dlg))
logWrapper.nextLogLevelEnd()
(...)
有没有办法从Python的解析器中获取当前的缩进级别,或者构造一个范围敏感的包装类来进行日志记录?