I am using python 2.7 for a multi-module project. As mentioned here I have configured logging in my main.py like below
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
filename='/home/ubuntu/logs/centralized_upgrade.log',
level=logging.INFO)
In every other module wherever I have to log I am just making a call like below
import logging
...
logging.info('Hello World')
But the doubt I am having is if in some module if root logger is again reconfigured (may be something like below)
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
filename='/home/ubuntu/logs/module.log',
level=logging.INFO)
then all log messages will start going into the new file. Can someone let me know what is the best way to handle this? How can I prevent the reconfiguration of root logger? My intention is that logging configuration should stay same as defined in main.py
irrespective of any reconfiguration done in any child modules.
In many places, it is recommended to not use root logger and define a logger like as mentioned here. But as per my understanding in this case also if some module configures the root logger then log may go to a different file as all loggers inherit from root logger.