Skip to content

Feature request: Add BaseException.__notes__ to log messages #6441

@waffleshop

Description

@waffleshop

Use case

As a developer, I would like PowerTools' Logging to support the notes feature (BaseException.__notes__) so my log messages can be enriched with data and code natively available via Python exceptions.

Solution/User Experience

PowerTools' log messages include a notes: list[str] attribute populated by Python's BaseException.__notes__.

The common use case is when you want to add context to a potential exception but not explicitly log at the time.

try:
    get_data(customer_id)
except Exception as exc:
    exc.add_note(f"Customer: '{customer_id}'")
    raise

Ideally, the log message would then include a notes attribute:

{
    "level": "ERROR",
    "location": "update_all_customers:64",
    "message": "Super detailed message here.",
    "timestamp": "2025-04-10 15:00:27,218+0000",
    "service": "CustomerManagement",
    "cold_start": true,
    "function_name": "customer_profile_updater",
    "function_memory_size": "512",
    "function_arn": "arn:aws:lambda:us-west-2:111111111111111:function:customer_profile_updater",
    "function_request_id": "b0635544-37e1-4407-8366-35b0c86222e5",
    "taskName": "Task-1",
    "xray_trace_id": "7812696e-9dc6-4909-b314-3d7c2c93da04",
    "notes": [
        "Encountered errors while updating customer profiles.",
        "Customer: '123456'"
    ]
}

Alternative solutions

PowerTools' Logging feature already includes alternate solutions but they're specific to PowerTools. It would be great if PowerTools' Logger leveraged the `BaseException.__notes__` attribute already available.

Acknowledgment

  • This feature request meets
    Should this be considered in other Powertools for AWS Lambda languages? i.e. , , and

Activity

leandrodamascena

leandrodamascena commented on Apr 10, 2025

@leandrodamascena
Contributor

Hi @waffleshop! Thanks for opening this issue! Yes, I think it makes a lot of sense to serialize the notes added when logging an exception or error with exc_info=True.

Notes are supported in Python 3.11+ and Powertools supports 3.9+, so we need to add a condition when serializing the exception to check if the Python version is 3.11+ or just if the __notes__ field exists in that object.

Would you like to submit a PR to implement this? I'd love to have your contribution here.

added and removed
triagePending triage from maintainers
on Apr 10, 2025
moved this from Backlog to Working on it in Powertools for AWS Lambda (Python)on Apr 15, 2025

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

    Participants

    @leandrodamascena@waffleshop

    Issue actions

      Feature request: Add BaseException.__notes__ to log messages · Issue #6441 · aws-powertools/powertools-lambda-python