示例#1
0
def test_create_logging_reporter_given_a_logger(mocker):
    ctor = mocker.patch(f"{PKG}.LoggingReporter", return_value=sentinel.reporter)

    reporter = create_logging_reporter(logger=sentinel.logger)
    assert reporter is reporter

    ctor.assert_called_once_with(sentinel.logger)
示例#2
0
def create_logging_reporting_listener(
    reporter: Optional[LoggingReporter] = None,
    logger: Optional[logging.Logger] = None,
    logger_name: str = "",
    level: Status = Status.SUCCESS,
    handlers: Optional[Iterable[logging.Handler]] = None,
    formatter: Optional[logging.Formatter] = None,
) -> LoggingReportingListener:
    """
    Create a logging reporting listener.

    Args:
        reporter: A reporter. When given this, the other parameters are ignored.
        logger: A logger where reports logged.
            When given this, `logger_name`, `level`, `handlers` and `formatter` are ignored.
        logger_name: The logger name. When not given, it will be automatically generated.
        level: The minimum level to report.
        handlers: The logging handlers. When given, `formatter` is ignored.
        formatter: The logging formatter.
    """
    reporter = reporter or create_logging_reporter(
        logger=logger,
        logger_name=logger_name,
        level=level,
        handlers=handlers,
        formatter=formatter,
    )
    return LoggingReportingListener(reporter)
示例#3
0
def test_create_logging_reporter_given_logger_elements(mocker, level: Status, expected_level: int):
    ctor = mocker.patch(f"{PKG}.LoggingReporter", return_value=sentinel.reporter)

    logger_name = str(uuid4())
    reporter = create_logging_reporter(
        logger_name=logger_name,
        level=level,
        handlers=iter((sentinel.handler,)),
    )
    assert reporter is reporter

    logger = logging.getLogger(logger_name)
    ctor.assert_called_once_with(logger)
    assert logger.level == expected_level
    assert logger.handlers == [sentinel.handler]
示例#4
0
def test_create_logging_reporter_given_no_parameters(mocker):
    uuid = NonCallableMagicMock(uuid4)
    uuid.__str__.return_value = __name__
    mocker.patch("uuid.uuid4", return_value=uuid)
    mocker.patch(f"{PKG}.LoggingReporter", return_value=sentinel.reporter)

    reporter = create_logging_reporter()
    assert reporter is reporter

    logger = logging.getLogger(__name__)
    assert logger.level == logging.INFO

    handlers = logger.handlers
    assert len(handlers) == 1
    handler = handlers[0]
    assert isinstance(handler, logging.StreamHandler)
    assert handler.stream is sys.stdout
    assert handler.level == logging.INFO
示例#5
0
def test_create_logging_reporter_given_no_handlers(mocker):
    mocker.patch(f"{PKG}.LoggingReporter", return_value=sentinel.reporter)

    logger_name = str(uuid4())
    reporter = create_logging_reporter(
        logger_name=logger_name,
        level=Status.UNSTABLE,
        formatter=sentinel.formatter,
    )
    assert reporter is reporter

    handlers = logging.getLogger(logger_name).handlers
    assert len(handlers) == 1
    handler = handlers[0]
    assert isinstance(handler, logging.StreamHandler)
    assert handler.stream is sys.stdout
    assert handler.level == logging.WARNING
    assert handler.formatter is sentinel.formatter