Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
waffleshop opened this issue Apr 10, 2025 · 3 comments · Fixed by #6465
Closed
1 of 2 tasks

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

waffleshop opened this issue Apr 10, 2025 · 3 comments · Fixed by #6465
Assignees
Labels

Comments

@waffleshop
Copy link

waffleshop commented Apr 10, 2025

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

@leandrodamascena
Copy link
Contributor

leandrodamascena commented Apr 10, 2025

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.

@leandrodamascena leandrodamascena added logger and removed triage Pending triage from maintainers labels Apr 10, 2025
@leandrodamascena leandrodamascena self-assigned this Apr 14, 2025
@leandrodamascena leandrodamascena linked a pull request Apr 15, 2025 that will close this issue
7 tasks
@leandrodamascena leandrodamascena moved this from Backlog to Working on it in Powertools for AWS Lambda (Python) Apr 15, 2025
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (Python) Apr 15, 2025
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Apr 15, 2025
Copy link
Contributor

This is now released under 3.11.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Coming soon
Development

Successfully merging a pull request may close this issue.

2 participants