Skip to content

feat(logger): add support for exception notes #6465

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

Merged
merged 5 commits into from
Apr 15, 2025

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #6441

Summary

This PR adds support for exception notes in the logger. When using logger.exception, any notes attached to exceptions will now be propagated and included in the log line under a new exception_notes key.

Exception notes provide additional context for errors, making debugging easier and logs more informative.

User experience

import requests

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

ENDPOINT = "http://httpbin.org/status/500"
logger = Logger(serialize_stacktrace=False)

try:
    ret = requests.get(ENDPOINT)
    ret.raise_for_status()
except requests.HTTPError as e:
    e.add_note("Can't connect to the endpoint")
    logger.exception(e)
    raise RuntimeError("Unable to fullfil request") from e

and the output:

{
    "level": "ERROR",
    "location": "collect.handler:15",
    "message": "Received a HTTP 5xx error",
    "timestamp": "2021-05-03 11:47:12,494+0000",
    "service": "payment",
    "exception_name": "RuntimeError",
    "exception": "Traceback (most recent call last):\n  File \"<input>\", line 2, in <module> RuntimeError: Unable to fullfil request",
    "exception_notes":[
       "Can't connect to the endpoint"
    ]
}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team as a code owner April 15, 2025 11:57
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation logger tests labels Apr 15, 2025
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 15, 2025
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Apr 15, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2025
@leandrodamascena leandrodamascena self-assigned this Apr 15, 2025
@leandrodamascena leandrodamascena linked an issue Apr 15, 2025 that may be closed by this pull request
2 tasks
@leandrodamascena
Copy link
Contributor Author

Hey @anafalcao! Can you please review this?

Copy link

codecov bot commented Apr 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.33%. Comparing base (7482311) to head (5150288).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6465   +/-   ##
========================================
  Coverage    96.32%   96.33%           
========================================
  Files          243      243           
  Lines        11824    11826    +2     
  Branches       881      881           
========================================
+ Hits         11390    11392    +2     
  Misses         338      338           
  Partials        96       96           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2025
Copy link
Contributor

@anafalcao anafalcao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR! APPROVED 🚀

@leandrodamascena leandrodamascena merged commit 40272b6 into develop Apr 15, 2025
14 checks passed
@leandrodamascena leandrodamascena deleted the feat/add-exceptions-notes-logging branch April 15, 2025 16:53
leandrodamascena added a commit that referenced this pull request Apr 24, 2025
* Adding support for exception notes

* Adding support for exception notes

* Python...

---------

Co-authored-by: Ana Falcão <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality logger size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add BaseException.__notes__ to log messages
2 participants