-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): log uncaught exceptions via system's exception hook (#1727
- Loading branch information
1 parent
2b4740a
commit d4a6726
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import requests | ||
|
||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
ENDPOINT = "http://httpbin.org/status/500" | ||
logger = Logger(log_uncaught_exceptions=True) | ||
|
||
|
||
def handler(event: dict, context: LambdaContext) -> str: | ||
ret = requests.get(ENDPOINT) | ||
# HTTP 4xx/5xx status will lead to requests.HTTPError | ||
# Logger will log this exception before this program exits non-successfully | ||
ret.raise_for_status() | ||
|
||
return "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"level": "ERROR", | ||
"location": "log_uncaught_exception_hook:756", | ||
"message": "Uncaught exception", | ||
"timestamp": "2022-11-16 13:51:29,198+0100", | ||
"service": "payment", | ||
"exception": "Traceback (most recent call last):\n File \"<input>\", line 52, in <module>\n handler({}, {})\n File \"<input>\", line 17, in handler\n ret.raise_for_status()\n File \"<input>/lib/python3.9/site-packages/requests/models.py\", line 1021, in raise_for_status\n raise HTTPError(http_error_msg, response=self)\nrequests.exceptions.HTTPError: 500 Server Error: INTERNAL SERVER ERROR for url: http://httpbin.org/status/500", | ||
"exception_name": "HTTPError" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters