import logging import os def setup_logger(name, log_file, level=logging.INFO): """Function setup as many loggers as you want""" formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') handler = logging.FileHandler(log_file) handler.setFormatter(formatter) # Also add a stream handler for console output stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger = logging.getLogger(name) logger.setLevel(level) logger.addHandler(handler) logger.addHandler(stream_handler) # If the file already exists, clear its contents to start fresh if os.path.exists(log_file): open(log_file, 'w').close() return logger