import logging, coloredlogs

def logsetup(filename, format, loglevel):
    fh = logging.FileHandler(filename)
    fh.setLevel(loglevel)

    sh = logging.StreamHandler()
    sh.setLevel(loglevel)

    formatter = coloredlogs.ColoredFormatter(fmt=format, field_styles={
        "hostname": {"color": "blue"},
        "programname": {"color": "cyan"},
        "name": {"color": "red"},
        "levelname": {"color": "magenta"},
        "asctime": {"color": "cyan"}
    })

    no_color_formatter = logging.Formatter(format)

    fh.setFormatter(no_color_formatter)
    sh.setFormatter(formatter)

    logging.basicConfig(handlers=[sh, fh], level=loglevel)

    return logging.getLogger("spotify-token-refresher")
