'Imports course using management command and captures logging output
at debug level for display in template'

def import_mongo_course(self, gitloc, branch):

    msg = u''
    log.debug('Adding    course    using    git    repo    %s', gitloc)
    output = StringIO.StringIO()
    import_log_handler = logging.StreamHandler(output)
    import_log_handler.setLevel(logging.DEBUG)
    logger_names = ['xmodule.modulestore.xml_importer', 'dashboard.git_import', 'xmodule.modulestore.xml', 'xmodule.seq_module']
    loggers = []
    for logger_name in logger_names:
        logger = logging.getLogger(logger_name)
        logger.setLevel(logging.DEBUG)
        logger.addHandler(import_log_handler)
        loggers.append(logger)
    error_msg = ''
    try:
        git_import.add_repo(gitloc, None, branch)
    except GitImportError as ex:
        error_msg = str(ex)
    ret = output.getvalue()
    for logger in loggers:
        logger.setLevel(logging.NOTSET)
        logger.removeHandler(import_log_handler)
    if error_msg:
        msg_header = error_msg
        color = 'red'
    else:
        msg_header = _('Added    Course')
        color = 'blue'
    msg = u"<h4    style='color:{0}'>{1}</h4>".format(color, msg_header)
    msg += u'<pre>{0}</pre>'.format(escape(ret))
    return msg
