Skip to content

Exception Group Traceback not fully visible using StdlibFormatter #185

@peterhorvat

Description

@peterhorvat

Description

When logging a nested Exception Group traceback from within a custom middleware, only the first Exception Group appears to be captured in the logs. The complete error traceback—including nested or inner Exception Groups—is not recorded. It is unclear whether this is due to incorrect usage, a limitation in the formatter, or an issue with the logging integration.

Steps to Reproduce

Raise a nested Exception Group inside a FastAPI middleware.

Let FastAPI/Uvicorn with ECS logging enabled handle and log the exception.

Observe that only the first Exception Group appears in the logs, rather than the entire nested traceback.

Expected Behavior

The full nested traceback, including all Exception Groups, should be visible in the logs for proper diagnostics and troubleshooting.

Actual Behavior

Only the first Exception Group is recorded in the ECS logs, while deeper nested exceptions are missing.

Environment

  • Python: 3.9
  • fastapi: 0.99.1
  • uvicorn: 0.15
  • ecs-logging: 1.1.0

Logger init code snippet

import ecs_logging
import logging
import uvicorn

log_handler = logging.StreamHandler()
log_handler.setFormatter(ecs_logging.StdlibFormatter())
logging.basicConfig(handlers=[log_handler])

ecs_log_config = uvicorn.config.LOGGING_CONFIG
ecs_log_config['formatters']['ecs_logging'] = {
        '()': 'ecs_logging.StdlibFormatter'
}
ecs_log_config['handlers']['default']['formatter'] = 'ecs_logging'
ecs_log_config['handlers']['access']['formatter'] = 'ecs_logging'

uvicorn.run("app.api:fastapi_app", host=<API_HOST>, port=<API_PORT>, reload=API_HOT_RELOAD, log_config=ecs_log_config)

Additional Context

This issue specifically arises when exceptions are raised and logged within FastAPI middleware.

Example error sample using uvicorn native LOGGING_CONFIG

ERROR:    Exception in ASGI application
+ Exception Group Traceback (most recent call last):
|   File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
|     result = await app(  # type: ignore[func-returns-value]
|   File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
|     return await self.app(scope, receive, send)
|   File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 290, in __call__
|     await super().__call__(scope, receive, send)
|   File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 122, in __call__
|     await self.middleware_stack(scope, receive, send)
|   File "/usr/local/lib/python3.9/site-packages/elasticapm/instrumentation/packages/asyncio/starlette.py", line 48, in call
|     return await wrapped(*args, **kwargs)
|   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
|     raise exc
|   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in __call__
|     await self.app(scope, receive, _send)
|   File "/usr/local/lib/python3.9/site-packages/elasticapm/contrib/starlette/__init__.py", line 199, in __call__
|     await self.app(scope, _request_receive or receive, wrapped_send)
|   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 91, in __call__
|     await self.simple_response(scope, receive, send, request_headers=headers)
|   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 146, in simple_response
|     await self.app(scope, receive, send)
|   File "/usr/local/lib/python3.9/site-packages/ratelimit/core.py", line 96, in __call__
|     return await self.app(scope, receive, send)
|   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 110, in __call__
|     response_sent.set()
|   File "/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
|     raise BaseExceptionGroup(
| exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
  | Exception Group Traceback (most recent call last):
  |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 108, in __call__
  |     response = await self.dispatch_func(request, call_next)
  |   File "/code/app/core/middlewares/middlewares.py", line 51, in dispatch
  |     return await call_next(request)
  |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 84, in call_next
  |     raise app_exc
  |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 70, in coro
  |     await self.app(scope, receive_or_disconnect, send_no_error)
  |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 110, in __call__
  |     response_sent.set()
  |   File "/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 78, in call_next
    |     message = await recv_stream.receive()
    |   File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 111, in receive
    |     return self.receive_nowait()
    |   File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 104, in receive_nowait
    |     raise EndOfStream
    | anyio.EndOfStream
    |
    | During handling of the above exception, another exception occurred:
    |
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 108, in __call__
    |     response = await self.dispatch_func(request, call_next)
    |   File "/code/app/core/api/security.py", line 6, in dispatch
    |     response = await call_next(request)
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 84, in call_next
    |     raise app_exc
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 70, in coro
    |     await self.app(scope, receive_or_disconnect, send_no_error)
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/authentication.py", line 48, in __call__
    |     await self.app(scope, receive, send)
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    |     raise exc
    |   File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    |     await self.app(scope, receive, sender)
    |   File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    |     raise e
    |   File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    |     await self.app(scope, receive, send)
    |   File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 718, in __call__
    |     await route.handle(scope, receive, send)
    |   File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
    |     await self.app(scope, receive, send)
    |   File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
    |     response = await func(request)
    |   File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 241, in app
    |     raw_response = await run_endpoint_function(
    |   File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 167, in run_endpoint_function
    |     return await dependant.call(**values)
    |   File "/code/app/api/routers/users.py", line 1171, in logout
    |     session_id = await authorization_service.get_session_id_from_access_token(request.headers.get('authorization'))
    |   File "/code/app/core/keycloak/authorization.py", line 163, in get_session_id_from_access_token
    |     return openid_connect.decode_token(token=token, key=key).get('sid')
    |   File "/usr/local/lib/python3.9/site-packages/keycloak/keycloak_openid.py", line 379, in decode_token
    |     return jwt.decode(token, key, algorithms=algorithms, audience=self.client_id, **kwargs)
    |   File "/usr/local/lib/python3.9/site-packages/jose/jwt.py", line 169, in decode
    |     _validate_claims(
    |   File "/usr/local/lib/python3.9/site-packages/jose/jwt.py", line 495, in _validate_claims
    |     _validate_aud(claims, audience=audience)
    |   File "/usr/local/lib/python3.9/site-packages/j̇ose/jwt.py", line 362, in _validate_aud
    |     raise JWTClaimsError("Invalid audience")
    | jose.exceptions.JWTClaimsError: Invalid audience

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions