将ExtendedInterpolation与Logging.Config.FileConfig.一起使用



当在ini文件中加载到Logging.config.FileConfig.时,我正在寻找一种方法来使用configparserlib中的ExtendedInterpolation功能

http://docs.python.org/3/library/configparser#configparser.ExtendedInterpolation

因此,如果我有一个ini文件,看起来像这样:

[logSettings]
eventlogs=application
logfilepath=C:Programsdk_testresultsdklog_009.log
levelvalue=10
[formatters]
keys=dkeventFmt,dklogFmt
[handlers]
keys=dklogHandler
[handler_dklogHandler]
class=FileHandler
level=${logSettings:levelvalue}
formatter=dklogFmt
args=(${logSettings:logfilepath}, 'w')
[logger_dklog]
level=${logSettings:levelvalue}
handlers=dklogHandler

正如您所看到的,我使用${…}表示法来引用不同部分中的值,从而遵循扩展的插值语法。当调用像logging.config.fileConfig(filepath)这样的文件时,模块内的求值总是失败。关于[handler_dklogHandler]部分中args选项的评估。

有办法绕过这个吗?谢谢

注意:使用Python 3.2

决定在文件上强制插值,并将结果保存到另一个临时文件中。我使用日志配置的临时文件。

功能如下:

tmpConfigDict           = {}
tmpConfig               = ConfigParser(allow_no_value = True,
interpolation = ExtendedInterpolation())
for path in configPaths:
tmpConfig.read(path)
#Iterate over options and use "get()" to execute the Interpolation
for sec in tmpConfig.sections():
tmpConfigDict[sec] = {}
for opt, _ in tmpConfig[sec].items():
tmpConfigDict[sec][opt] = cleanValue(tmpConfig.get(sec, opt))
#Finished getting values. Write the dict to the configparser
tmpConfig.read_dict(tmpConfigDict)
#Open the file handle and close it when done
with open(pathToTmpFile, 'w') as fp:
tmpConfig.write(fp, space_around_delimiters = False)

最新更新