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

bee*_*ech 6 python logging interpolation

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

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

所以,如果我有一个看起来像这样的ini文件:

[logSettings]
eventlogs=application
logfilepath=C:\Programs\dk_test\results\dklog_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
Run Code Online (Sandbox Code Playgroud)

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

有办法解决这个问题吗?谢谢!

注意:使用Python 3.2

bee*_*ech 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)
Run Code Online (Sandbox Code Playgroud)