Halvor Holsten Strand
Halvor Holsten Strand

Reputation: 20536

Python logging file config with variable directory

I'm using Python logging and loading it with logging.config.fileConfig(config_file), from e.g. logging.cfg. It contains a TimedRotatingFileHandler. It works great. For a long time it has been using (simplied):

[handler_file]
class=handlers.TimedRotatingFileHandler
formatter=complex
level=DEBUG
args=('logs/logging.log', 'midnight')

Now I want to make the logging directory (logs above) configurable, but still use the configuration file, e.g. logging.cfg.

I was hoping to somehow inject/string interpolate in a variable e.g. log_directory to just do:

args=('%(log_directory)s/logging.log', 'midnight')

Or since logging does an eval maybe something like:

args=(f'{log_directory}/logging.log', 'midnight')

I have a feeling I'm missing something simple. How can I best do this, while still using a configuration file?

Upvotes: 1

Views: 287

Answers (1)

Vinay Sajip
Vinay Sajip

Reputation: 99315

You can specify defaults in the fileConfig() call:

logging.config.fileConfig('logging.cfg', defaults={
    'log_directory': 'foo/logs'
}, disable_existing_loggers=False)

along with

args=('%(log_directory)s/logging.log', 'midnight')

Of course, make sure the directory you specify exists and is writable.

Upvotes: 1

Related Questions