Skip to content

Bug: Logger(json_serializer=json.dumps) cannot log subclassed exceptions #6185

@fraser-langton

Description

@fraser-langton

Expected Behaviour

Traceback (most recent call last):
  File "/Users/.../temp.py", line 12, in <module>
    raise MyException
MyException

Current Behaviour

--- Logging error ---
Traceback (most recent call last):
  File "/path/to/project/temp.py", line 12, in <module>
    raise MyException
MyException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/python/lib/python3.12/logging/__init__.py", line 1160, in emit
    msg = self.format(record)
          ^^^^^^^^^^^^^^^^^^^
  File "/path/to/python/lib/python3.12/logging/__init__.py", line 999, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "/path/to/project/.venv/lib/python3.12/site-packages/aws_lambda_powertools/logging/formatter.py", line 201, in format
    return self.serialize(log=formatted_log)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/.venv/lib/python3.12/site-packages/aws_lambda_powertools/logging/formatter.py", line 183, in serialize
    return self.json_serializer(log)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/python/lib/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/python/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/python/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/path/to/python/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type MyException is not JSON serializable
Call stack:
  File "/path/to/project/temp.py", line 14, in <module>
    logger.exception("Exception occurred")
  File "/path/to/project/.venv/lib/python3.12/site-packages/aws_lambda_powertools/logging/logger.py", line 508, in exception
    return self._logger.exception(
Message: 'Exception occurred'
Arguments: ()

Code snippet

from aws_lambda_powertools import Logger

logger = Logger()

class MyException(Exception): ...

try:
    raise MyException
except MyException:
    logger.exception("Exception occurred")

Possible Solution

No response

Steps to Reproduce

import json

from aws_lambda_powertools import Logger

logger = Logger(json_serializer=json.dumps)


class MyException(Exception): ...


try:
    raise MyException
except MyException:
    logger.exception("Exception occurred")

Powertools for AWS Lambda (Python) version

sentry-sdk==2.22.0

AWS Lambda function runtime

3.11

Packaging format used

Lambda Layers

Debugging logs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions