示例#1
0
def setup_logger():
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    formatter = JsonFormatter()
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger.addHandler(handler)
def test_undesired_base_attributes_are_not_in_json(base_attribute):
    record = _build_log_record()

    actual_json_string = JsonFormatter().format(record)
    actual = json.loads(actual_json_string)

    assert base_attribute not in actual
def test_timestamp_is_included_in_json():
    record = _build_log_record(created=1607965513.358049)
    actual_json_string = JsonFormatter().format(record)
    actual = json.loads(actual_json_string)["time"]

    expected = "2020-12-14T17:05:13.358049"

    assert actual == expected
def test_extra_field_is_included_in_json():
    record = _build_log_record(extra={"fruit": "mango"})

    actual_json_string = JsonFormatter().format(record)
    actual = json.loads(actual_json_string)["fruit"]

    expected = "mango"

    assert actual == expected
def test_desired_base_attributes_are_included_in_json(json_field,
                                                      base_attribute):
    value = a_string()
    record = _build_log_record(**{base_attribute: value})

    actual_json_string = JsonFormatter().format(record)
    actual = json.loads(actual_json_string)[json_field]

    assert value == actual
def test_extra_fields_is_included_in_json():
    record = _build_log_record(extra={"fruit": "mango", "colour": "red"})

    actual_json_string = JsonFormatter().format(record)
    actual_json = json.loads(actual_json_string)

    actual_fruit = actual_json["fruit"]
    actual_colour = actual_json["colour"]

    expected_fruit = "mango"
    expected_colour = "red"

    assert actual_fruit == expected_fruit and actual_colour == expected_colour