teal.logging – Logging utilities for TeaL applications#

class teal.logging.LoggingFastAPI(*, debug: bool = False, routes: ~typing.List[~starlette.routing.BaseRoute] | None = None, title: str = 'FastAPI', description: str = '', version: str = '0.1.0', openapi_url: str | None = '/openapi.json', openapi_tags: ~typing.List[~typing.Dict[str, ~typing.Any]] | None = None, servers: ~typing.List[~typing.Dict[str, ~typing.Any | str]] | None = None, dependencies: ~typing.Sequence[~fastapi.params.Depends] | None = None, default_response_class: ~typing.Type[~starlette.responses.Response] = <fastapi.datastructures.DefaultPlaceholder object>, docs_url: str | None = '/docs', redoc_url: str | None = '/redoc', swagger_ui_oauth2_redirect_url: str | None = '/docs/oauth2-redirect', swagger_ui_init_oauth: ~typing.Dict[str, ~typing.Any] | None = None, middleware: ~typing.Sequence[~starlette.middleware.Middleware] | None = None, exception_handlers: ~typing.Dict[int | ~typing.Type[Exception], ~typing.Callable[[~starlette.requests.Request, ~typing.Any], ~typing.Coroutine[~typing.Any, ~typing.Any, ~starlette.responses.Response]]] | None = None, on_startup: ~typing.Sequence[~typing.Callable[[], ~typing.Any]] | None = None, on_shutdown: ~typing.Sequence[~typing.Callable[[], ~typing.Any]] | None = None, lifespan: ~typing.Callable[[~fastapi.applications.AppType], ~typing.AsyncContextManager[None]] | ~typing.Callable[[~fastapi.applications.AppType], ~typing.AsyncContextManager[~typing.Mapping[str, ~typing.Any]]] | None = None, terms_of_service: str | None = None, contact: ~typing.Dict[str, ~typing.Any | str] | None = None, license_info: ~typing.Dict[str, ~typing.Any | str] | None = None, openapi_prefix: str = '', root_path: str = '', root_path_in_servers: bool = True, responses: ~typing.Dict[int | str, ~typing.Dict[str, ~typing.Any]] | None = None, callbacks: ~typing.List[~starlette.routing.BaseRoute] | None = None, deprecated: bool | None = None, include_in_schema: bool = True, swagger_ui_parameters: ~typing.Dict[str, ~typing.Any] | None = None, generate_unique_id_function: ~typing.Callable[[~fastapi.routing.APIRoute], str] = <fastapi.datastructures.DefaultPlaceholder object>, **extra: ~typing.Any)#

Bases: FastAPI

FastAPI application with the logging middleware at top level.

build_middleware_stack() Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]]#

Build the middleware stack.

This is overridden to have the logging middleware at top of the middleware stack, to also handle exceptions automatically.


The wrapped ASGI app.

class teal.logging.LoggingMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]])#

Bases: BaseHTTPMiddleware

Middleware for setting logging utilities per request.

async dispatch(request: Request, call_next: Callable[[Request], Awaitable[Response]]) Response#

Dispatch the request.

  • request – The request to dispatch.

  • call_next – The ASGI app to call next.


The next request.

class teal.logging.RequestFormatter(*args, **kwargs)#

Bases: JsonFormatter

Formatter for all logs from TeaL.

gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,

tm_sec, tm_wday, tm_yday, tm_isdst)

Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT). When ‘seconds’ is not passed in, convert the current time instead.

If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only.

class teal.logging.RequestLoggingContext(*, request_id: str | None = None)#

Bases: BaseModel

Logging properties.

request_id: str | None#

Identifier for the current request.

teal.logging.get_current_context() RequestLoggingContext | None#

Get the current logging context.


A reference to the current logging context.

teal.logging.make_log_record(*, args: tuple[Any, ...], kwargs: dict[str, Any], original_factory: Callable[[...], LogRecord]) LogRecord#

Make a log record with contextual information.

  • args – The positional arguments to the factory.

  • kwargs – The keyword arguments to the factory.

  • original_factory – The original factory to call first.


The log record to pass to the handlers.