0

Mainly this is a question about how to get a similar behaviour as java logging, where it is enough to create or edit the logging config file to change the behaviour of the logging.

Is this possible in Python without having to modify the scripts which are using logging functions?

2 Answers 2

1

As long as the python program uses the .fileConfig method from the logging module, then yes, you can alter logging configuration by editing a file.

Generally, the main function of a larger program would call it like this:

import logging

DEFAULT_LOG_CONFIG = 'somepath/logging.conf'

def main(args):
    ...
    # parse command line arguments or similar
    log_config = options.logconfig or DEFAULT_LOG_CONFIG
    logging.fileConfig(DEFAULT_LOG_CONFIG)

The documentation on the file format is pretty comprehensive.

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

Comments

0

Martijn is right (upvoted), but if you are using versions 2.7/3.2 or later of Python, then you might consider the newer dictionary-based configuration which offers better coverage of logging featured than the older .ini file-based configuration Martijn mentions. The dictionary schema is also reasonably well documented. Dictionary-based configuration is available for older Python versions at the price of an extra dependency, via the logutils package.

Logging's configuration functions are intended to cover exactly the use case mentioned: you can even arrange for long running programs such as servers to be reconfigured on the fly, without needing to be restarted.

2 Comments

But the dictionary-based config does not describe a file format.
@Martijn: It does not directly describe a format, but the mappings to e.g. JSON and XML files are self-evident.

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.