I have a python project where I have 3 python files out of which app.py is the main file and other two files are tracker.py and celerio.py. I have the logging enabled for all the files and below is its code:
def setup_logger(logger_name, log_file, level=logging.ERROR):
log_formatter = logging.Formatter('%(asctime)s %(message)s')
my_handler = RotatingFileHandler(log_file, mode='a', maxBytes=100 * 1024 * 1024, backupCount=5, encoding=None,
delay=0)
my_handler.setFormatter(log_formatter)
my_handler.setLevel(level)
l = logging.getLogger(logger_name)
l.addHandler(my_handler)
dir = Path(__file__).parents[1]
log_file = os.path.join(dir, 'logs', 'application.log')
setup_logger('debug', log_file)
log = logging.getLogger('debug')
and then I use it like log.error("Hello world") in the python file and it works fine. Now as I have another 2 files, I have also copied the same above code in those two files tracker.py and celerio.py. But the problem is, lets if I log log.error("Hello world from app.py") in app.py. It is saved as 3 times in application.log file. Same is the case with other files as well.
2019-11-15 13:32:00,288 Hello world from app.py
2019-11-15 13:32:00,288 Hello world from app.py
2019-11-15 13:32:00,288 Hello world from app.py
I dont know why it is logged 3 times for just 1 log. Can anyone please help me in this. Thanks