import logging


def _set_logger(
    logger_file_path: str,
    logger_name: str = "default_logger",
    write_to_console: bool = True,
) -> logging.Logger:
    """Set logger to log to the given path.

    Modified from https://docs.python.org/3/howto/logging-cookbook.html

    Args:
        logger_file_path (str): Filepath to write to
        logger_name (str, optional): Name of the logger to use. Defaults
            to "default_logger"
        write_to_console (bool, optional): Should write the logs to console.
            Defaults to True

    Returns:
        logging.Logger: Logger object
    """
    logger = logging.getLogger(name=logger_name)
    logger.setLevel(level=logging.INFO)
    # create file handler which logs all the messages
    file_handler = logging.FileHandler(filename=logger_file_path)
    file_handler.setLevel(level=logging.INFO)
    # create formatter and add it to the handlers
    formatter = logging.Formatter(fmt="%(message)s")
    file_handler.setFormatter(fmt=formatter)
    # add the handlers to the logger
    logger.addHandler(hdlr=file_handler)

    if write_to_console:
        # create console handler with a higher log level
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(level=logging.INFO)
        # add formatter to the handlers
        stream_handler.setFormatter(fmt=formatter)
        # add the handlers to the logger
        logger.addHandler(hdlr=stream_handler)
    return logger