I want all warnings to be handled by the way I want logs to be handled. Even these emitted during importing libraries.
This means the configuration of the logging must be done before importing libraries.
After searching some time I think configuring logging in a custom sitecustomize.py
could be a solution.
But sitecustomize.py
is somehow black magic only few people know it, and even less people use it.
Is there a more obvious way to ensure that my logging config is done before libraries get imported?
Unfortunately there seems to be no way to configure logging via environment variables.
Update
I got no answer which is acceptable according to my point of view. I think you need to split logging into two areas of responsibility:
- Environment: set up logging: Which format, which files ...
- Code: use logging. Do emit info, warn and error messages.
As soon as the first line of code gets executed by the python interpreter, the area "environment" has given the responsibility to the code. Now it is too late to do any configuration. I would like to see setting up the logging to be a part of calling the python interpreter.
One solution could be an environment variable:
PYTHON_LOGGING_CONFIG=/path-to-env/etc/config.yaml
or an argument to the interpreter:
python --logging-config=path-to-env/etc/config.yaml script.py