Skip to content
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

feat(logger): log uncaught exceptions via system's exception hook #1727

Merged

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Nov 16, 2022

Issue number: #1636

Summary

Changes

Please provide a summary of what's being changed

This PR introduces a new constructor flag in Logger, log_uncaught_exceptions, to add a custom exception hook to log any uncaught exception using customer's Logger.

User experience

Please share what the user experience looks like before and after this change

from aws_lambda_powertools import Logger

logger = Logger(service="sample", log_uncaught_exceptions=True)

raise ValueError("Catch me if you can!")

Leads to ValueError uncaught exception being logged with Logger.

{
    "level": "ERROR",
    "location": "log_uncaught_exception_hook:757",
    "message": "Uncaught exception",
    "timestamp": "2022-11-16 11:58:39,819+0100",
    "service": "sample",
    "exception": "Traceback (most recent call last):\n  File \"/Users/lessa/DEV/aws-lambda-powertools-python/blah.py\", line 38, in <module>\n    raise ValueError(\"Catch me if you can!\")\nValueError: Catch me if you can!",
    "exception_name": "ValueError"
}

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.

@heitorlessa heitorlessa requested a review from a team as a code owner November 16, 2022 11:17
@heitorlessa heitorlessa requested review from rubenfonseca and removed request for a team November 16, 2022 11:17
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 16, 2022
@github-actions github-actions bot added the feature New feature or functionality label Nov 16, 2022
@codecov-commenter
Copy link

codecov-commenter commented Nov 16, 2022

Codecov Report

Base: 99.27% // Head: 99.27% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (e62dbc8) compared to base (2b4740a).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1727   +/-   ##
========================================
  Coverage    99.27%   99.27%           
========================================
  Files          129      129           
  Lines         6079     6085    +6     
  Branches       406      407    +1     
========================================
+ Hits          6035     6041    +6     
  Misses          20       20           
  Partials        24       24           
Impacted Files Coverage Δ
aws_lambda_powertools/logging/logger.py 99.41% <100.00%> (+0.02%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Nov 16, 2022
@heitorlessa heitorlessa linked an issue Nov 16, 2022 that may be closed by this pull request
2 tasks
@heitorlessa
Copy link
Contributor Author

@rubenfonseca and @leandrodamascena are unavailable, so merging after due diligence. Only difficulty here is to create a functional test to simulate an uncaught exception, so I tested that Python's exception hook uses our Logger IF there were an uncaught exception.

@heitorlessa heitorlessa merged commit d4a6726 into aws-powertools:develop Nov 16, 2022
@heitorlessa heitorlessa deleted the feat/logger-exception-hook branch November 16, 2022 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation 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: Automatically log exceptions using sys.eventhook
2 participants