Ejemplo n.º 1
0
def test_logger_log_with_stacklevel_below_stack_depth(level, logger, handler,
                                                      all_attr_fmt):
    # Given
    fmt = 'greeting: %s'
    args = ('hello', )
    formatter = jsonlogging.Formatter(fmt=all_attr_fmt, style='{')
    handler.setFormatter(formatter)

    # When
    log_and_quit_when_depth_reached(10,
                                    logger,
                                    level,
                                    fmt,
                                    *args,
                                    stack_info=True,
                                    stack_level=5)

    # Then
    logs = list(handler.level_logs(level))
    assert len(logs) == 1
    log_entry = json.loads(logs[0])
    assert 'exc_info' in log_entry
    assert 'frames' in log_entry['stack_info']
    assert isinstance(log_entry['stack_info']['frames'], list)
    assert len(log_entry['stack_info']['frames']) == 5
Ejemplo n.º 2
0
def test_jsonlogging_with_partial_compiler(all_attr_fmt, benchmark, kwargs,
                                           level, null_handler, null_logger):
    # Given
    null_handler.setFormatter(jsonlogging.Formatter(fmt=all_attr_fmt))
    logging_tree.printout()

    # Then
    benchmark.pedantic(null_logger.log,
                       args=(level, 'message'),
                       kwargs=kwargs,
                       rounds=100,
                       iterations=100)
Ejemplo n.º 3
0
def test_logger_log_with_stack_info_equal_true_format_stacks_false(
        level, logger, handler, all_attr_fmt):
    # Given
    fmt = 'greeting: %s'
    args = ('hello', )
    formatter = jsonlogging.Formatter(fmt=all_attr_fmt, style='{')
    handler.setFormatter(formatter)

    # When
    logger.log(level, fmt, *args, stack_info=True)
    log_entry = json.loads(list(handler.level_logs(level))[0])

    # Then
    assert 'stack_info' in log_entry
    assert isinstance(log_entry['stack_info'], str)
Ejemplo n.º 4
0
def test_format_stack_without_relative_paths(all_attr_fmt, handler, logger):
    # Given
    formatter = jsonlogging.Formatter(all_attr_fmt,
                                      style='{',
                                      relative_paths=False,
                                      format_stacks=True)
    handler.setFormatter(formatter)

    # When
    logger.debug('debug message', stack_info=True)

    # Then
    log_entry = json.loads(list(handler.debug_logs)[0])
    assert 'stack_info' in log_entry
    assert 'frames' in log_entry['stack_info']
    for filename, _, _, _ in log_entry['stack_info']['frames']:
        assert '/' == filename[0]
Ejemplo n.º 5
0
def test_logger_log_with_exc_info_equal_true(level, logger, handler,
                                             all_attr_fmt):
    # Given
    fmt = 'greeting: %s'
    args = ('hello', )
    formatter = jsonlogging.Formatter(fmt=all_attr_fmt, style='{')
    handler.setFormatter(formatter)

    # When
    logger.log(level, fmt, *args, exc_info=True)
    log_entry = json.loads(list(handler.level_logs(level))[0])

    # Then
    assert 'exc_info' in log_entry
    assert 'frames' in log_entry['exc_info']
    assert isinstance(log_entry['exc_info']['frames'], list)
    assert 'value' in log_entry['exc_info']
    assert log_entry['exc_info']['value'] is None
Ejemplo n.º 6
0
def test_logger_with_extra(logger, handler, all_attr_fmt):
    # Given
    fmt = 'greeting: %s'
    args = ('hello', )
    formatter = jsonlogging.Formatter(fmt=all_attr_fmt, style='{')
    handler.setFormatter(formatter)

    # When
    logger.debug(fmt, *args, extra={'k': 'v'})
    logger.info(fmt, *args, extra={'k': 'v'})
    logger.warning(fmt, *args, extra={'k': 'v'})
    logger.error(fmt, args, extra={'k': 'v'})
    try:
        raise ValueError('Exception type does not really matter, but do not'
                         ' use `Exception` or your linter will complain.')
    except ValueError:
        logger.exception(fmt, *args, extra={'k': 'v'})

    # Then
    expected_keys = jsonlogging.LOG_RECORD_ATTRS + ('k', )
    for log in handler.logs:
        log = json.loads(log)
        assert list(log.keys()) == list(expected_keys)