using configobj string interpolation and logging.config.dictConfig

Peter Otten __peter__ at web.de
Thu May 25 17:15:52 EDT 2017


Tim Williams wrote:

> On Wednesday, May 24, 2017 at 5:47:37 PM UTC-4, Peter Otten wrote:
>> Tim Williams wrote:
>> 
>> > Just as a followup, if I use 'unrepr=True' in my ConfigObj, I don't
>> > have to convert the strings.
>> 
>> I'd keep it simple and would use JSON...
> 
> I looked at JSON at first, but went with configobj because I didn't see
> where it did string interpolation, which I needed for other parts of my
> INI file, and I'm trying to use it to specify my log file in my handler.
> 
> Which brings me to ...

> I have this stripped down INI file:
> 
...

How do you get

> LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'

to be interpolated while leaving

> format = '%(asctime)s: (%(levelname)s)  %(message)s'

as is?

> However, when I try to call logging.config.dictConfig() on it, the stream
> that is opened on creating the logging.FileHandler object is
> "%(LogFile)s", not "C:/TestData/test.log".

I don't even get this far:

>>> c = configobj.ConfigObj("second.ini", unrepr=True)
>>> c.dict()
Traceback (most recent call last):
...
configobj.MissingInterpolationOption: missing option "asctime" in 
interpolation.

I tried to escape % as %%, but that doesn't seem to work. When I provide 
bogus replacements

>>> c = configobj.ConfigObj("third.ini", unrepr=True)
>>> pprint.pprint(c.dict()["loggng"])
{'CaptureDrive': 'C:/',
 'LogFile': 'C:/TestData/test.log',
 'RootDir': 'TestData',
 'asctime': 'ASCTIME',
 'formatters': {'fmt1': {'datefmt': '',
                         'format': 'ASCTIME: (LEVELNAME)  MESSAGE'}},
 'handlers': {'console': {'class': 'logging.StreamHandler',
                          'level': 'INFO',
                          'stream': 'ext://sys.stdout'},
              'file': {'class': 'logging.FileHandler',
                       'filename': 'C:/TestData/test.log',
                       'level': 'WARN'}},
 'level': 'INFO',
 'levelname': 'LEVELNAME',
 'loggers': {'root': {'handlers': ['file', 'console'], 'level': 'INFO'}},
 'message': 'MESSAGE',
 'version': 1}

I get the expected output.




More information about the Python-list mailing list