I have this logger class I created and it works perfectly for a project I have but when I simply copy and paste the structure to another project it doesn't work. The logs I try to write do not appear in the file specified nor in the console.
This is the code:
import logging
class Logger:
def __init__(self, path=None, name=None):
format_string = '%(asctime)s : T%(relativeCreated)05ds : %(levelname)-8s - %(name)s : %(message)s'
format_date = '%Y-%m-%dT%H:%M:%S'
# BASIC CONFIG
if path is not None:
logging.basicConfig(filename=path, filemode='a', format=format_string, datefmt=format_date)
# START LOGGER
logger = logging.getLogger(name)
if path is None:
# DEFINE FORMAT
handler = logging.StreamHandler()
formatter = logging.Formatter(fmt=format_string, datefmt=format_date)
handler.setFormatter(formatter)
logger.addHandler(handler)
# LEVEL SET
logger.setLevel(logging.DEBUG)
logger.info('TEST')
self.logger = logging.getLogger(__name__)
self.logger.info(f'START NEW EXECUTION\n\n{"=" * 36}\n')
The line self.logger.info(f'START NEW EXECUTION\n\n{"=" * 36}\n') doesn't print anything to the file set by the variable path. And this only happens when I add a name to the logger. I added this name variable as a suggestion I found in order to avoid writing logs of Python Modules.
I feel like I am doing something very simple (I have just started using the Logging Module) that is causing this problem but I can't see it since this exact structure works fine in another project.
EDIT0: My final goal is to have a Logger Class I can use in any project just by copying the file and using it the same way, if anyone has something like this and wouldn't mind sharing, then it would be enough for me.