예제 #1
0
def setup_logging(suite, args, defaults=None):
    """
    Configure a structuredlogger based on command line arguments.

    The created structuredlogger will also be set as the default logger, and
    can be retrieved with :py:func:`get_default_logger`.

    :param suite: The name of the testsuite being run
    :param args: A dictionary of {argument_name:value} produced from
                 parsing the command line arguments for the application
    :param defaults: A dictionary of {formatter name: output stream} to apply
                     when there is no logging supplied on the command line. If
                     this isn't supplied, reasonable defaults are chosen
                     (coloured mach formatting if stdout is a terminal, or raw
                     logs otherwise).

    :rtype: StructuredLogger
    """
    logger = StructuredLogger(suite)
    prefix = "log_"
    found = False
    found_stdout_logger = False
    if not hasattr(args, 'iteritems'):
        args = vars(args)

    if defaults is None:
        if sys.__stdout__.isatty():
            defaults = {"mach": sys.stdout}
        else:
            defaults = {"raw": sys.stdout}

    for name, values in args.iteritems():
        if name.startswith(prefix) and values is not None:
            for value in values:
                found = True
                if isinstance(value, str):
                    value = log_file(value)
                if value == sys.stdout:
                    found_stdout_logger = True
                formatter_cls = log_formatters[name[len(prefix):]][0]
                logger.add_handler(handlers.StreamHandler(stream=value,
                                                          formatter=formatter_cls()))

    #If there is no user-specified logging, go with the default options
    if not found:
        for name, value in defaults.iteritems():
            formatter_cls = log_formatters[name][0]
            logger.add_handler(handlers.StreamHandler(stream=value,
                                                      formatter=formatter_cls()))

    elif not found_stdout_logger and sys.stdout in defaults.values():
        for name, value in defaults.iteritems():
            if value == sys.stdout:
                formatter_cls = log_formatters[name][0]
                logger.add_handler(handlers.StreamHandler(stream=value,
                                                          formatter=formatter_cls()))

    set_default_logger(logger)

    return logger
예제 #2
0
def setup_logging(suite, args, defaults):
    """
    Configure a structuredlogger based on command line arguments.

    The created structuredlogger will also be set as the default logger, and
    can be retrieved with :py:func:`get_default_logger`.

    :param suite: The name of the testsuite being run
    :param args: A dictionary of {argument_name:value} produced from
                 parsing the command line arguments for the application
    :param defaults: A dictionary of {formatter name: output stream} to apply
                     when there is no logging supplied on the command line.

    :rtype: StructuredLogger
    """
    logger = StructuredLogger(suite)
    prefix = "log_"
    found = False
    found_stdout_logger = False
    if not hasattr(args, 'iteritems'):
        args = vars(args)
    for name, values in args.iteritems():
        if name.startswith(prefix) and values is not None:
            for value in values:
                found = True
                if isinstance(value, str):
                    value = log_file(value)
                if value == sys.stdout:
                    found_stdout_logger = True
                formatter_cls = log_formatters[name[len(prefix):]][0]
                logger.add_handler(
                    handlers.StreamHandler(stream=value,
                                           formatter=formatter_cls()))

    #If there is no user-specified logging, go with the default options
    if not found:
        for name, value in defaults.iteritems():
            formatter_cls = log_formatters[name][0]
            logger.add_handler(
                handlers.StreamHandler(stream=value,
                                       formatter=formatter_cls()))

    elif not found_stdout_logger and sys.stdout in defaults.values():
        for name, value in defaults.iteritems():
            if value == sys.stdout:
                formatter_cls = log_formatters[name][0]
                logger.add_handler(
                    handlers.StreamHandler(stream=value,
                                           formatter=formatter_cls()))

    set_default_logger(logger)

    return logger
예제 #3
0
def setup_logging(suite, args, defaults):
    """
    Configure a structuredlogger based on command line arguments.

    :param suite: The name of the testsuite being run
    :param args: A dictionary of {argument_name:value} produced from
                 parsing the command line arguments for the application
    :param defaults: A dictionary of {formatter name: output stream} to apply
                     when there is no logging supplied on the command line.

    :rtype: StructuredLogger
    """
    logger = StructuredLogger(suite)
    prefix = "log_"
    found = False
    found_stdout_logger = False
    for name, values in args.iteritems():
        if name.startswith(prefix) and values is not None:
            for value in values:
                found = True
                if value == sys.stdout:
                    found_stdout_logger = True
                formatter_cls = log_formatters[name[len(prefix):]][0]
                logger.add_handler(handlers.StreamHandler(stream=value,
                                                          formatter=formatter_cls()))

    #If there is no user-specified logging, go with the default options
    if not found:
        for name, value in defaults.iteritems():
            formatter_cls = log_formatters[name][0]
            logger.add_handler(handlers.StreamHandler(stream=value,
                                                      formatter=formatter_cls()))

    elif not found_stdout_logger and sys.stdout in defaults.values():
        for name, value in defaults.iteritems():
            if value == sys.stdout:
                formatter_cls = log_formatters[name][0]
                logger.add_handler(handlers.StreamHandler(stream=value,
                                                          formatter=formatter_cls()))

    return logger
예제 #4
0
def setup_logging(suite, args, defaults):
    """
    Configure a structuredlogger based on command line arguments.

    :param suite: The name of the testsuite being run
    :param args: The Namespace object produced by argparse from parsing
                 command line arguments from a parser with logging arguments.
    :param defaults: A dictionary of {formatter name: output stream} to apply
                     when there is no logging supplied on the command line.

    :rtype: StructuredLogger
    """
    logger = StructuredLogger(suite)
    prefix = "log_"
    found = False
    found_stdout_logger = False
    for name, value in args.iteritems():
        if name.startswith(prefix) and value is not None:
            found = True
            if value == sys.stdout:
                found_stdout_logger = True
            formatter_cls = log_formatters[name[len(prefix):]][0]
            logger.add_handler(handlers.StreamHandler(stream=value,
                                                      formatter=formatter_cls()))

    #If there is no user-specified logging, go with the default options
    if not found:
        for name, value in defaults.iteritems():
            formatter_cls = log_formatters[name][0]
            logger.add_handler(handlers.StreamHandler(stream=value,
                                                      formatter=formatter_cls()))

    elif not found_stdout_logger and sys.stdout in defaults.values():
        for name, value in defaults.iteritems():
            if value == sys.stdout:
                formatter_cls = log_formatters[name][0]
                logger.add_handler(handlers.StreamHandler(stream=value,
                                                          formatter=formatter_cls()))

    return logger