views:

30

answers:

1

I have a BASE_DIR setting in my settings.py file:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

I need to use this variable in my logging.ini file to setup my file handler paths.

The initialization of logging happens in the same file, the settings.py file, below my BASE_DIR variable. Here I tell it the path of my logging.ini file:

LOG_INIT_DONE=False
if not LOG_INIT_DONE:
   logging.config.fileConfig(LOGGING_INI)
   LOG_INIT_DONE=True

I noticed fileConfig can take a default parameter. I'm not sure if this is what I'm looking for, but I can't seem to find any documentation on how to use this parameter.

Thanks, Pete

+2  A: 

As per the docs,

logging.fileConfig(fname[, defaults])

Reads the logging configuration from a ConfigParser-format file named fname. This function can be called several times from an application, allowing an end user the ability to select from various pre-canned configurations (if the developer provides a mechanism to present the choices and load the chosen configuration). Defaults to be passed to ConfigParser can be specified in the defaults argument.

So you can simply pass as the second argument a dictionary like

{'basedir': BASE_DIR}

and then just interpolate its basedir entry in your logging.ini file:

[SomeSection]
somefile: %(basedir)s/foobar.txt

and the like!

Alex Martelli
Thanks Alex. I actually had it working by the time you responded; however, in all fairness, no where does it say that parameter expects a dictionary. Additionally, it doesn't mention that is parses the config file replacing keys in defaults dict.
slypete
@slypete, the `logging.fileConfig` docs, as above quoted, mention `ConfigParser`, and the `ConfigParser` docs are the ones that say that `defaults` is a dict and is used in interpolation (so if you're not 100% familiar with `ConfigParser`, when the `logging` docs mention it you'll clearly go look at the `ConfigParser` docs and find out the details -- wouldn't make sense for the `logging` docs to repeat all those details, though a hyperlink to `ConfigParser` _would_ make sense... normally the online docs do hyperlink, but, I believe, not in this specific case;-).
Alex Martelli