avoid registering duplicate log handlers

init_logging is called many times in test_config,
which has been registering numerous duplicate log handlers,
attached to stderr that's closed between tests
This commit is contained in:
Min RK
2023-03-24 11:30:43 +01:00
parent a5e72046ab
commit 5da2859408

View File

@@ -9,6 +9,13 @@ def init_logging():
"""Setup default tljh logger""" """Setup default tljh logger"""
logger = logging.getLogger("tljh") logger = logging.getLogger("tljh")
os.makedirs(INSTALL_PREFIX, exist_ok=True) os.makedirs(INSTALL_PREFIX, exist_ok=True)
# check if any log handlers are already registered
# don't reconfigure logs if handlers are already configured
# e.g. happens in pytest, which hooks up log handlers for reporting
# or if this function is called twice
if logger.hasHandlers():
return
file_logger = logging.FileHandler(os.path.join(INSTALL_PREFIX, "installer.log")) file_logger = logging.FileHandler(os.path.join(INSTALL_PREFIX, "installer.log"))
file_logger.setFormatter(logging.Formatter("%(asctime)s %(message)s")) file_logger.setFormatter(logging.Formatter("%(asctime)s %(message)s"))
logger.addHandler(file_logger) logger.addHandler(file_logger)