Is it possible to log into multiple log files from a single module in python 3.0, where the logs file are named based on some request parameter while using flask framework.
below code works fine if i run it like a single module and import but when i run using flask, it writes in the first attempt but later falls back to logging to the root logger.
I want a logging factory in flask that can check if the logger is already present and if already present than log in the same file and if not then create a new logger and log to the new file.
Any help is greatly appreciated.
def setup_logger( name, log_file, level=logging.INFO):
my_file = Path(log_file)
print(my_file)
if my_file.is_file():
print("handler details")
print(logging.getLogger(name).hasHandlers())
print(type(logging.getLogger(name).hasHandlers()))
if logging.getLogger(name).hasHandlers():
print("old logger and it has handler")
logger.propagate = False
return logging.getLogger(name)
else:
handler = logging.FileHandler(log_file, mode='a')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
logger.propagate = False
print("old logger that has no handler")
return logger
else:
handler = logging.FileHandler(log_file, mode='a')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
print("new logger with new handler")
logger.propagate = False
return logger