How to log the contents of a ConfigParser?

As this is the top Google search result and I was hoping to find a solution to print the values of the ConfigParser instance to stdout, here's a one-liner to help all future readers:

print({section: dict(config[section]) for section in config.sections()})

You should be able to create a writable object that writes to the log. Something like this (if you want to keep the string around you could modify the ConfigLogger to save it as well):

import ConfigParser
import logging

class ConfigLogger(object):
    def __init__(self, log):
        self.__log = log
    def __call__(self, config):
        self.__log.info("Config:")
        config.write(self)
    def write(self, data):
        # stripping the data makes the output nicer and avoids empty lines
        line = data.strip()
        self.__log.info(line)

config = ConfigParser.ConfigParser()
config.add_section("test")
config.set("test", "a", 1)
# create the logger and pass it to write
logging.basicConfig(filename="test.log", level=logging.INFO)
config_logger = ConfigLogger(logging)
config_logger(config)

This yields the following output:

INFO:root:Config:
INFO:root:[test]
INFO:root:a = 1
INFO:root: