slypete
slypete

Reputation: 5648

How do I use django settings in my logging.ini file?

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

Upvotes: 1

Views: 1548

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881555

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!

Upvotes: 3

Related Questions