def init_testcase_logging(name, verbose=0, silent=False, color=False,
                          truncate=True):
    """ Initialize testcase logging and default loggers. First removes any
    existing testcase loggers, creates a new directory for testcase logs under
    the root logging directory, initializes the default bench logger and
    removes any child loggers it may have.
    :param name: Name of the testcase
    :param verbose: Log level debug
    :param silent: Log level warning
    :param color: Log coloring
    :param truncate: Log truncating
    """
    global LOGTCDIR
    global LOGTCNAME
    global LOGGERS
    global REPEATNUM
    global VERBOSE_LEVEL
    global SILENT_ON
    global TRUNCATE_LOG

    if name is None or not isinstance(name, str) or not name:
        raise EnvironmentError("{} is invalid testcase name for logging configuration".format(name))

    if STANDALONE_LOGGING:
        init_base_logging()

    finish_testcase_logging()

    # Generate filename friendly base name for logs from testcase name
    LOGTCDIR = re.sub(r'[^\w\-_\. ]', '_', name)
    LOGTCNAME = LOGTCDIR
    tempname = os.path.join(LOGPATHDIR, LOGTCDIR + "_0")

    # Check if tc folder already exists. If it exists, we look for a new folder
    # by appending integers to the folder name
    while os.path.exists(tempname):
        tempname = os.path.join(LOGPATHDIR, LOGTCDIR + "_{}".format(REPEATNUM))
        REPEATNUM = REPEATNUM + 1
    LOGTCDIR = tempname
    REPEATNUM = 0

    if not os.path.exists(LOGTCDIR):
        os.makedirs(LOGTCDIR)

    # Get the bench logger and remove all existing handlers
    benchlogger = logging.getLogger("bench")
    benchlogger.propagate = False
    remove_handlers(benchlogger)
    benchlogger.setLevel(logging.DEBUG)

    TRUNCATE_LOG = truncate
    if TRUNCATE_LOG:
        benchlogger.addFilter(ContextFilter())

    # Console handler for bench
    stream_handler = logging.StreamHandler()
    if color and not COLORS:
        raise ImportError("Missing module: coloredlogs. Please install"
                          "with pip to use colors in logging.")
    stream_handler.formatter = BenchFormatterWithType(color)
    VERBOSE_LEVEL = verbose
    SILENT_ON = silent
    if verbose and not silent:
        stream_handler.setLevel(logging.DEBUG)
    elif silent:
        stream_handler.setLevel(logging.WARN)
    else:
        configs = LOGGING_CONFIG.get("Bench", {})
        stream_handler.setLevel(getattr(logging,
                                        configs.get("level", DEFAULT_LOGGING_CONFIG.get("level"))))
    benchlogger.addHandler(stream_handler)
    # file handler for bench and all child loggers
    filename = LOGGING_CONFIG.get("Bench",
                                  DEFAULT_LOGGING_CONFIG).get("file").get("name", "bench.log")
    benchlogger = _add_filehandler(benchlogger, get_testcase_logfilename(filename), name="Bench")
    LOGGERS["bench"] = BenchLoggerAdapter(benchlogger, {"source": "TC"})
def init_testcase_logging ( name , verbose = 0 , silent = False , color = False , truncate = True ) : global LOGTCDIR global LOGTCNAME global LOGGERS global REPEATNUM global VERBOSE_LEVEL global SILENT_ON global TRUNCATE_LOG if name is None or not isinstance ( name , str ) or not name : raise EnvironmentError ( "{} is invalid testcase name for logging configuration" . format ( name ) ) if STANDALONE_LOGGING : init_base_logging ( ) finish_testcase_logging ( ) LOGTCDIR = re . sub ( r'[^\w\-_\. ]' , '_' , name ) LOGTCNAME = LOGTCDIR tempname = os . path . join ( LOGPATHDIR , LOGTCDIR + "_0" ) while os . path . exists ( tempname ) : tempname = os . path . join ( LOGPATHDIR , LOGTCDIR + "_{}" . format ( REPEATNUM ) ) REPEATNUM = REPEATNUM + 1 LOGTCDIR = tempname REPEATNUM = 0 if not os . path . exists ( LOGTCDIR ) : os . makedirs ( LOGTCDIR ) benchlogger = logging . getLogger ( "bench" ) benchlogger . propagate = False remove_handlers ( benchlogger ) benchlogger . setLevel ( logging . DEBUG ) TRUNCATE_LOG = truncate if TRUNCATE_LOG : benchlogger . addFilter ( ContextFilter ( ) ) stream_handler = logging . StreamHandler ( ) if color and not COLORS : raise ImportError ( "Missing module: coloredlogs. Please install" "with pip to use colors in logging." ) stream_handler . formatter = BenchFormatterWithType ( color ) VERBOSE_LEVEL = verbose SILENT_ON = silent if verbose and not silent : stream_handler . setLevel ( logging . DEBUG ) elif silent : stream_handler . setLevel ( logging . WARN ) else : configs = LOGGING_CONFIG . get ( "Bench" , { } ) stream_handler . setLevel ( getattr ( logging , configs . get ( "level" , DEFAULT_LOGGING_CONFIG . get ( "level" ) ) ) ) benchlogger . addHandler ( stream_handler ) filename = LOGGING_CONFIG . get ( "Bench" , DEFAULT_LOGGING_CONFIG ) . get ( "file" ) . get ( "name" , "bench.log" ) benchlogger = _add_filehandler ( benchlogger , get_testcase_logfilename ( filename ) , name = "Bench" ) LOGGERS [ "bench" ] = BenchLoggerAdapter ( benchlogger , { "source" : "TC" } )
Initialize testcase logging and default loggers. First removes any
    existing testcase loggers, creates a new directory for testcase logs under
    the root logging directory, initializes the default bench logger and
    removes any child loggers it may have.
    :param name: Name of the testcase
    :param verbose: Log level debug
    :param silent: Log level warning
    :param color: Log coloring
    :param truncate: Log truncating