def init():
    """Init loggsetting. It should be called after flags.init."""
    loglevel = flags.OPTIONS.loglevel.lower()
    logdir = flags.OPTIONS.logdir
    logfile = flags.OPTIONS.logfile
    logger = logging.getLogger()
    if logger.handlers:
        for handler in logger.handlers:
            logger.removeHandler(handler)

    if logdir:
        if not logfile:
            logfile = '%s.log' % os.path.basename(sys.argv[0])

        handler = logging.handlers.TimedRotatingFileHandler(
            os.path.join(logdir, logfile),
            when=flags.OPTIONS.log_interval_unit,
            interval=flags.OPTIONS.log_interval,
            backupCount=flags.OPTIONS.log_backup_count)
    else:
        if not logfile:
            handler = logging.StreamHandler(sys.stderr)
        else:
            handler = logging.handlers.TimedRotatingFileHandler(
                logfile,
                when=flags.OPTIONS.log_interval_unit,
                interval=flags.OPTIONS.log_interval,
                backupCount=flags.OPTIONS.log_backup_count)

    if loglevel in LOGLEVEL_MAPPING:
        logger.setLevel(LOGLEVEL_MAPPING[loglevel])
        handler.setLevel(LOGLEVEL_MAPPING[loglevel])

    formatter = logging.Formatter(
        flags.OPTIONS.log_format)

    handler.setFormatter(formatter)
    logger.addHandler(handler)
dataset/ETHPy150Open openstack/compass-core/compass/utils/logsetting.py/init/original