예제 #1
0
파일: conftest.py 프로젝트: mbeacom/kopf
def logstream(caplog):
    """ Prefixing is done at the final output. We have to intercept it. """

    logger = logging.getLogger()
    handlers = list(logger.handlers)

    # Setup all log levels of sub-libraries. A sife-effect: the handlers are also added.
    configure(verbose=True)

    # Remove any stream handlers added in the step above. But keep the caplog's handlers.
    for handler in list(logger.handlers):
        if isinstance(handler, logging.StreamHandler) and handler.stream is sys.stderr:
            logger.removeHandler(handler)

    # Inject our stream-intercepting handler.
    stream = io.StringIO()
    handler = logging.StreamHandler(stream)
    formatter = ObjectPrefixingTextFormatter('prefix %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    try:
        with caplog.at_level(logging.DEBUG):
            yield stream
    finally:
        logger.removeHandler(handler)
        logger.handlers[:] = handlers  # undo `configure()`
예제 #2
0
 def wrapper(verbose: bool, quiet: bool, debug: bool,
             log_format: loggers.LogFormat = loggers.LogFormat.FULL,
             log_prefix: Optional[bool] = False,
             log_refkey: Optional[str] = None,
             *args: Any, **kwargs: Any) -> Any:
     loggers.configure(debug=debug, verbose=verbose, quiet=quiet,
                       log_format=log_format, log_refkey=log_refkey, log_prefix=log_prefix)
     return fn(*args, **kwargs)
예제 #3
0
def test_levels(verbose, debug, quiet, expected_level):
    configure(verbose=verbose, debug=debug, quiet=quiet)
    logger = logging.getLogger()
    assert logger.level == expected_level
예제 #4
0
def test_error_on_unknown_formatter():
    with pytest.raises(ValueError):
        configure(log_format=object())
예제 #5
0
def test_json_has_no_prefix_by_default(log_format):
    configure(log_format=log_format, log_prefix=None)
    logger = logging.getLogger()
    own_handlers = _get_own_handlers(logger)
    assert len(own_handlers) == 1
    assert type(own_handlers[0].formatter) is ObjectJsonFormatter
예제 #6
0
def test_formatter_prefixed_json(log_format):
    configure(log_format=log_format, log_prefix=True)
    logger = logging.getLogger()
    own_handlers = _get_own_handlers(logger)
    assert len(own_handlers) == 1
    assert type(own_handlers[0].formatter) is ObjectPrefixingJsonFormatter
예제 #7
0
def test_formatter_nonprefixed_text(log_format):
    configure(log_format=log_format, log_prefix=False)
    logger = logging.getLogger()
    own_handlers = _get_own_handlers(logger)
    assert len(own_handlers) == 1
    assert type(own_handlers[0].formatter) is ObjectTextFormatter
예제 #8
0
def test_own_formatter_is_used():
    configure()
    logger = logging.getLogger()
    own_handlers = _get_own_handlers(logger)
    assert len(own_handlers) == 1