Example #1
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
Example #2
0
def setup_handlers(logger, formatters, formatter_options):
    """
    Add handlers to the given logger according to the formatters and
    options provided.

    :param logger: The logger configured by this function.
    :param formatters: A dict of {formatter, [streams]} to use in handlers.
    :param formatter_options: a dict of {formatter: {option: value}} to
                              to use when configuring formatters.
    """
    unused_options = set(formatter_options.keys()) - set(formatters.keys())
    if unused_options:
        msg = ("Options specified for unused formatter(s) (%s) have no effect" %
               list(unused_options))
        raise ValueError(msg)

    for fmt, streams in formatters.iteritems():
        formatter_cls = log_formatters[fmt][0]
        formatter = formatter_cls()
        handler_wrapper, handler_option = None, ""
        for option, value in formatter_options[fmt].iteritems():
            if option == "buffer":
                handler_wrapper, handler_option = fmt_options[option][0], value
            else:
                formatter = fmt_options[option][0](formatter, value)

        for value in streams:
            handler = handlers.StreamHandler(stream=value, formatter=formatter)
            if handler_wrapper:
                handler = handler_wrapper(handler, handler_option)
            logger.add_handler(handler)
Example #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: 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