1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.