@contextmanager
def log_config(level, file_=None):
    """
    A context manager to configure logging and then undo the configuration.

    Undoing the configuration is useful for testing, since otherwise
    many log handlers might accumulate during the course of testing,
    due to successive calls to this method.

    Arguments:

      level: lowest logging level to log.
      file_: the file object to use for logging (e.g. sys.stderr).

    """
    if level is None:
        level = LOG_LEVEL_DEFAULT
    root = logging.getLogger()
    # If logging was already configured (e.g. at the outset of a test run),
    # then let's not change the root logging level.
    # TODO: simplify this logic (e.g. we should not need "if" logic).
    already_configured = root.hasHandlers()
    handler = make_log_handler(level, file_=file_)
    root.addHandler(handler)
    if not already_configured:
        root.setLevel(level)
        log.debug("root logger level set to: %r" % logging.getLevelName(level))
    log.debug("a logging handler was added")
    yield
    root.removeHandler(handler)
dataset/ETHPy150Open cjerdonek/open-rcv/openrcv/scripts/run.py/log_config/original