Skip to content

Bug(logging): appended keys do not get captured by caplog #6991

@amin-farjadi

Description

@amin-farjadi

Expected Behaviour

The following test should pass

def test_logger_with_caplog_with_appended_keys(caplog):
    caplog.set_level("INFO")
    logger = Logger(service="abcd")
    logger.info("testing, testing...")
    logger.append_keys(
        context={
            "testing": "true",
        },
    )
    logger.info("testing, testing, now with context...")
    assert len(caplog.records) == 2
    assert caplog.records[0].message == "testing, testing..."
    assert caplog.records[1].message == "testing, testing, now with context..."
    assert caplog.records[1].context == {"testing": "true"}

Current Behaviour

The above test fails with error AttributeError: 'LogRecord' object has no attribute 'context'.

But, the test below passes:

def test_logger_with_caplog_with_extra(caplog, service_name):
    caplog.set_level("INFO")
    logger = Logger(service=service_name)
    logger.info("testing, testing...")
    logger.thread_safe_append_keys(
        context={
            "testing": "true",
        },
    )
    logger.info(
        "testing, testing, now with extra...",
        extra={
            "context": {"testing": "true"},
        },
    )
    assert len(caplog.records) == 2
    assert caplog.records[0].message == "testing, testing..."
    assert caplog.records[1].message == "testing, testing, now with extra..."
    assert caplog.records[1].context == {"testing": "true"}

Code snippet

Provided above ^

Possible Solution

No response

Steps to Reproduce

  • Add tests above to tests/functional/logger/required_dependencies/test_logger.py
  • Run make test

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Coming soon

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions