import logging


def get_task_logger(txn_id, task_id, loglevel='INFO'):
    """A factory for making a logger that contains a client transaction id, and
    a task id.

    :Returns: logging.Logger

    :param txn_id: The client-supplied transaction id
    :type tx_id: String

    :param task_id: The id of the current task
    :type task_id: String
    """
    extra = {}
    extra['txn_id'] = txn_id
    extra['task_id'] = task_id
    logger = logging.getLogger(task_id)
    logger.setLevel(loglevel.upper())
    if not logger.handlers:
        ch = logging.StreamHandler()
        formatter = logging.Formatter('%(asctime)s [%(txn_id)s] [%(task_id)s]: %(message)s')
        ch.setLevel(loglevel.upper())
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    logger = logging.LoggerAdapter(logger, extra)
    return logger