-
Notifications
You must be signed in to change notification settings - Fork 432
Bug: Logger(json_serializer=json.dumps) cannot log subclassed exceptions #6185
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
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hey @fraser-langton, thanks for opening this issue! When using Powertools without a custom serializer, we can automatically serialize exceptions and return a formatted JSON. However, when you're directly using I'm curious about your specific requirements for the JSON serializer. Could you provide more context about what functionality you're looking to implement that isn't currently working with Powertools? Just in case If you want to do this, you must create a CustomJSONEncoder that understand this object and serialize it. Something like this: import json
import traceback
from aws_lambda_powertools import Logger
class CustomJSONEncoder(json.JSONEncoder):
def default(self, obj):
# Handle exceptions
if isinstance(obj, BaseException):
return {
'type': type(obj).__name__,
'message': str(obj),
'traceback': traceback.format_exc()
}
# Handle objects with __dict__
if hasattr(obj, '__dict__'):
return obj.__dict__
# For any other non-serializable objects, convert to string
return str(obj)
# Custom JSON serializer using the custom encoder
def custom_json_serializer(obj):
return json.dumps(obj, cls=CustomJSONEncoder)
logger = Logger(json_serializer=custom_json_serializer)
class MyException(Exception): ...
try:
raise MyException
except MyException:
logger.exception("Exception occurred") Thanks. |
Ok thanks for the added context, my assumption was the Exception was converted to dict form first regardless of the serialiser. We incorrectly assumed json dumps was the default as we have a different serialiser for different environments so we just reverted to using the default. Thanks you! |
|
Expected Behaviour
Current Behaviour
Code snippet
Possible Solution
No response
Steps to Reproduce
Powertools for AWS Lambda (Python) version
sentry-sdk==2.22.0
AWS Lambda function runtime
3.11
Packaging format used
Lambda Layers
Debugging logs
The text was updated successfully, but these errors were encountered: