import logging
import torch.distributed as dist

def create_logger(logging_dir):
    """
    Create a logger that writes to a log file and stdout.
    """
    try:
        rank_ = dist.get_rank()
    except Exception as err:
        rank_ = 0

    if rank_ == 0:  # real logger
        logging.basicConfig(
            level=logging.INFO,
            format='[\033[34m%(asctime)s\033[0m] %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S',
            handlers=[logging.StreamHandler(), logging.FileHandler(f"{logging_dir}/log.txt")]
        )
        logger = logging.getLogger(__name__)
    else:  # dummy logger (does nothing)
        logger = logging.getLogger(__name__)
        logger.addHandler(logging.NullHandler())
    return logger