import logging


def setup_logger(name: str, log_file: str, mode: str, level: int, log_format: logging.Formatter) -> logging.Logger:
    """
    Setup a logger with a given name and log file.
    Arguments:
        name {str} : Name of the logger.
        log_file {str} : Path to the log file.
        level {int} : Log level.
    Returns:
        The logger (logging.Logger)
    """
    try:
        handler = logging.FileHandler(log_file, mode=mode)
    except FileNotFoundError:
        try:
            # If find is not found, just create it and continue
            file = open(log_file, "a+")
            file.write(f"# Log name: {name}")
            handler = logging.FileHandler(log_file, mode=mode)
        except FileNotFoundError:
            # We cannot fix this so just reverse filename to a logger name and normally save it.
            handler = logging.FileHandler(f'{name}.log', mode=mode)
    handler.setFormatter(log_format)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger