Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ExtendedInterpolation with Logging.Config.FileConfig

I am looking for a way to use the ExtendedInterpolation functionality found in the configparser lib when loading in a ini file to Logging.config.FileConfig.

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

So if I have a ini file that looks like this:

[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

As you can see I am following the extended interpolation syntax by using the ${...} notation to reference a value in a different section. When calling the file like so logging.config.fileConfig(filepath), the eval'ing within the module always fails. In paticular on the eval'ing of the args option in the [handler_dklogHandler] section.

Is there a way to get around this? Thanks!

Note: Using Python 3.2

like image 599
beeryardtech Avatar asked Dec 11 '12 19:12

beeryardtech


1 Answers

Decided to use force the interpolation over the file and save the result to another temp file. I use the temp file for the logconfig.

The function looks like this:

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)
like image 85
beeryardtech Avatar answered Nov 05 '22 11:11

beeryardtech