views:

125

answers:

5

I have a python logger set up, using python's logging module. I want to store the string I'm using with the logging Formatter object in a configuration file using the ConfigParser module.

The format string is stored in a dictionary of settings in a separate file that handles the reading and writing of the config file. The problem I have is that python still tries to format the file and falls over when it reads all the logging-module-specific formatting flags.

{
    "log_level":logging.debug,
    "log_name":"C:\\Temp\\logfile.log",
    "format_string":
        "%(asctime)s %(levelname)s: %(module)s, line %(lineno)d - %(message)s"
}

My question is simple: how can I disable the formatting functionality here while keeping it elsewhere. My initial reaction was copious use of the backslash to escape the various percent symbols, but that of course permanently breaks the formatting such that it wont work even when I need it to.

I should also mention, since it was bought up in the comments, that ConfigParser does some internal interpolation that causes the trip-up. Here is my traceback:

Traceback (most recent call last):
  File "initialconfig.py", line 52, in <module>
    "%(asctime)s %(levelname)s: %(module)s, line %(lineno)d - %(message)s"
  File "initialconfig.py", line 31, in add_settings
    self.set(section_name, setting_name, default_value)
  File "C:\Python26\lib\ConfigParser.py", line 668, in set
    "position %d" % (value, m.start()))
ValueError: invalid interpolation syntax in '%(asctime)s %(levelname)s: %(module
)s, line %(lineno)d - %(message)s' at position 10

Also, general pointers on good settings-file practices would be nice. This is the first time I've done anything significant with ConfigParser (or logging for that matter).

Thanks in advance, Dominic

+3  A: 

Did you try to escape percents with %%?

wRAR
No luck. Still getting a ValueError complaining about invalid interpolation syntax.
dangerouslyfacetious
It turns out it was a bug in Python 2.6.2. Upgrading to 2.6.5 fixed the issue. This does exactly what is required.
dangerouslyfacetious
+1  A: 

What is the problem with the code above? Interpolation is only performed if the % operator is applied to the string. If you don't use % you can use the formatting string like any other string.

Fabian Jakobs
I should have mentioned - The ConfigParser module does some string interpolation internally and that is where it trips up. I'll edit my question with the traceback for clarity.
dangerouslyfacetious
+2  A: 

You might wanna use ConfigParser.RawConfigParser instead of ConfigParser.ConfigParser. Only the latter does magical interpolation on config values.

EDIT:

Actually, using ConfigParser.SafeConfigParser you'll able to escape format strings with an additional % percent sign. This example should be working then:

{
    "log_level":logging.debug,
    "log_name":"C:\\Temp\\logfile.log",
    "format_string":
        "%%(asctime)s %%(levelname)s: %%(module)s, line %%(lineno)d - %%(message)s"
}
Haes
That worked. Though what happens when I need to use the interpolation features?
dangerouslyfacetious
see my updated part of the answer.
Haes
A: 

There's RawConfigParser which is like ConfigParser without the interpolation behaviour. If you don't use the interpolation feature in any other part of the configuration file, you can simply replace ConfigParser with RawConfigParser in your code.

See the documentation of RawConfigParser for more details.

Tamás
+1  A: 

What version of Python are you using? An upgrade to 2.6.4 may help, see

http://bugs.python.org/issue5741

Peter Otten
That fixed it, thanks.
dangerouslyfacetious