Skip to content

Bug: Logger error with exec_info=True #6358

Closed

Description

@flatherskevin

Expected Behaviour

I would expected the output to be

AWS logger
NoneType: None

Current Behaviour

--- Logging error ---
Traceback (most recent call last):
  File "/Users/kevin.flathers/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/logging/__init__.py", line 1160, in emit
    msg = self.format(record)
          ^^^^^^^^^^^^^^^^^^^
  File "/Users/kevin.flathers/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/logging/__init__.py", line 999, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "/Users/kevin.flathers/codebase/cz-common-python/.venv/lib/python3.12/site-packages/aws_lambda_powertools/logging/formatter.py", line 197, in format
    extracted_exception, extracted_exception_name = self._extract_log_exception(log_record=record)
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kevin.flathers/codebase/cz-common-python/.venv/lib/python3.12/site-packages/aws_lambda_powertools/logging/formatter.py", line 391, in _extract_log_exception
    return self.formatException(log_record.exc_info), log_record.exc_info[0].__name__  # type: ignore
                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__name__'. Did you mean: '__ne__'?
Call stack:
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/kevin.flathers/codebase/cz-common-python/z.py", line 10, in <module>
    aws_logger.warning('AWS logger', exc_info=True)
  File "/Users/kevin.flathers/codebase/cz-common-python/.venv/lib/python3.12/site-packages/aws_lambda_powertools/logging/logger.py", line 544, in warning
    return self._logger.warning(
Message: 'AWS logger'
Arguments: ()

Code snippet

import logging
from aws_lambda_powertools import Logger


python_logger = logging.getLogger(__name__)
aws_logger = Logger()

python_logger.warning('Python logger', exc_info=True)
print('\n\n')
aws_logger.warning('AWS logger', exc_info=True)

Possible Solution

It appears like AWS Power Tools Logger is throwing an error somewhere when exc_info is set to True, but an exception was not raised. This is counter to how the Python logger works, where it just prints NoneType: None

Steps to Reproduce

Run the provided script

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

Activity

  1. added
    bug
    triage
    on Mar 27, 2025
  2. boring-cyborg commented on Mar 27, 2025

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  3. leandrodamascena commented on Mar 27, 2025

    @leandrodamascena
    Contributor

    Thank you for raising this issue to improve the developer experience @flatherskevin!

    You're correct. This behavior occurs in Powertools because setting exc_info triggers the addition of exception information to the logging message.

    To clarify:

    1. When exc_info is set, Powertools attempts to include exception details in the log.
    2. We also try to include a comprehensive stack tracer.

    I agree that we should test if an exception occurs before adding this key, but I'm curious to hear from you in which scenarios you want to log warning with exc_info=True without a real exception.

    Thanks.

  4. added and removed
    triage
    on Mar 27, 2025
  5. flatherskevin commented on Mar 27, 2025

    @flatherskevin
    Author

    @leandrodamascena the use case was mostly just we migrated from the Python logger to the AWS PowerTools Logger, and this situation showed up and started failing. We have fixed the line in our code because using exc_info outside of an exception is not necessarily useful, but I wanted to raise it here as it is a breaking bug from the core Python logging behavior.

  6. leandrodamascena commented on Mar 27, 2025

    @leandrodamascena
    Contributor

    @leandrodamascena the use case was mostly just we migrated from the Python logger to the AWS PowerTools Logger, and this situation showed up and started failing. We have fixed the line in our code because using exc_info outside of an exception is not necessarily useful, but I wanted to raise it here as it is a breaking bug from the core Python logging behavior.

    Hi @flatherskevin! Thanks a lot for clarifying the use case and I agree that sense for us to fix this.

    If you have some bandwidth, I'd be more than happy to have your input here. But if not, don't worry, I can fix this in the next release, which will be in 15 days.

  7. waffleshop commented on Mar 28, 2025

    @waffleshop

    I encountered the same issue when code defers exceptions by storing them in a list, logging them later, then raising them together as an ExceptionGroup.

  8. leandrodamascena commented on Mar 28, 2025

    @leandrodamascena
    Contributor

    I encountered the same issue when code defers exceptions by storing them in a list, logging them later, then raising them together as an ExceptionGroup.

    Hey @waffleshop! Can you share a snippet that I can reproduce this error?

  9. waffleshop commented on Mar 28, 2025

    @waffleshop

    @leandrodamascena Here is a vague snippet that reproduces my code's situation. I was able to work around the issue by using asyncio.as_completed() instead of asyncio.gather().

    import asyncio
    from random import randint
    
    from aws_lambda_powertools import Logger
    
    logger = Logger()
    
    
    async def random_sleep():
        """Simulates IO wait and errors"""
        if randint(1, 5) == 5:
            raise Exception("Simulated exception")
    
        await asyncio.sleep(randint(1, 5))
    
    
    async def main():
        """Main function to run the async tasks"""
        results = await asyncio.gather(
            *[random_sleep() for _ in range(20)], return_exceptions=True
        )
        exceptions = []
    
        for result in results:
            if isinstance(result, Exception):
                logger.exception(result)
                exceptions.append(result)
    
        if exceptions:
            raise ExceptionGroup("Some tasks failed", exceptions)
    
    
    if __name__ == "__main__":
        asyncio.run(main())
  10. waffleshop commented on Mar 28, 2025

    @waffleshop

    This is my workaround with asyncio.as_completed():

    import asyncio
    from random import randint
    
    from aws_lambda_powertools import Logger
    
    logger = Logger()
    
    
    async def random_sleep():
        """Simulates IO wait and errors"""
        if randint(1, 5) == 5:
            raise Exception("Simulated exception")
    
        await asyncio.sleep(randint(1, 5))
    
    
    async def main():
        """Main function to run the async tasks"""
        tasks = [asyncio.create_task(random_sleep()) for _ in range(20)]
    
        exceptions = []
    
        for coro in asyncio.as_completed(tasks):
            try:
                await coro
            except Exception as e:
                logger.exception(e)
                exceptions.append(e)
                continue
    
        if exceptions:
            raise ExceptionGroup("Some tasks failed", exceptions)
    
    
    if __name__ == "__main__":
        asyncio.run(main())
  11. moved this from Working on it to Backlog in Powertools for AWS Lambda (Python)on Mar 31, 2025
  12. moved this from Backlog to Working on it in Powertools for AWS Lambda (Python)on Apr 7, 2025
  13. leandrodamascena commented on Apr 7, 2025

    @leandrodamascena
    Contributor

    Hey @flatherskevin and @waffleshop thanks for all the explanation! I'm working in a PR to fix this and release tomorrow.

  14. github-actions commented on Apr 7, 2025

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY WARNING⚠️

    This issue is now closed. Please be mindful that future comments are hard for our team to see.

    If you need more assistance, please either tag a team member or open a new issue that references this one.

    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  15. added
    pending-release
    on Apr 7, 2025
  16. github-actions commented on Apr 8, 2025

    @github-actions
    Contributor

    This is now released under 3.10.0 version!

  17. removed
    pending-release
    on Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

buglogger

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions