from pathlib import Path
import logging


def make_a_logger(myinput: Path):
    """
        Creates a logger that will output to the console and to a log.txt file
        in the same folder as the file specified by 'myinput'.
    """

    # Create a custom logger
    # get root logger.  FYI, this "root logger" is called from experimental.py and other places via globals
    logger = logging.getLogger('')

    # don't duplicate handlers if we already have them
    if logger.hasHandlers() == False:
        log_filepath = Path(myinput.parent, 'log.txt')
        print(f'Logging output to: {log_filepath}')

        # Create handlers
        c_handler = logging.StreamHandler()
        f_handler = logging.FileHandler(log_filepath, mode='w')

        # Create formatters and add it to handlers
        c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
        c_handler.setFormatter(c_format)
        f_handler.setFormatter(c_format)

        # Add handlers to the logger
        logger.addHandler(c_handler)
        logger.addHandler(f_handler)
        logger.setLevel(logging.INFO)  # have to put the logging level here not at the handler level. sigh.

    return logger