I have a logger but in one python file, it always print out logging info twice. For example,
class Runner(object):
def __init__(self, model_list):
# Initialize based on list parameter
self.load(model_list)
logger.info(f'Runner is initialized ... {model_list}')
When I run my system, this logging info is printed twice as below:
2022-03-20 11:06:04,662 - nlu.matcher.runner - INFO - 30 - Runner is initialized ... [3]
2022-03-20 11:06:04,662 - nlu.matcher.runner - INFO - 30 - Runner is initialized ... [3]
I use the same logger in other python files, but others don't have the same issue. I looked at this link (Python Logger Logs Twice). The issue is similar but not exactly the same, so I added this line to the file which has the problem, and it didn't solve the problem
logger.propagate = False
What may be the reason for twice logging in a particular file? The following is my logger definition:
import logging
import logging.handlers
import os
from .configs.config import CONFIG
format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(message)s')
abspath = os.path.abspath(os.path.dirname(__file__))
logger_config = CONFIG['loggers']
logger_level = CONFIG['logger_level'][0]
def my_logger(module_name):
logger = logging.getLogger(module_name)
logger.setLevel(logger_level)
# stream handler
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.DEBUG)
c_handler.setFormatter(format)
# debug handler
f1_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/debug.log"))
f1_handler.setLevel(logging.DEBUG)
f1_handler.setFormatter(format)
# info handler
# f2_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/info.log"))
# f2_handler.setLevel(logging.INFO)
# f2_handler.setFormatter(format)
# warning handler
f3_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/warning.log"))
f3_handler.setLevel(logging.WARNING)
f3_handler.setFormatter(format)
# error handler
f4_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/error.log"))
f4_handler.setLevel(logging.ERROR)
f4_handler.setFormatter(format)
# Add handlers to the logger
if 'stream' in logger_config:
logger.addHandler(c_handler)
logger.addHandler(f1_handler)
# logger.addHandler(f2_handler)
logger.addHandler(f3_handler)
logger.addHandler(f4_handler)
return logger