logging#

class litestar.middleware.logging.LoggingMiddleware#

Bases: AbstractMiddleware

Logging middleware.

__init__(app: ASGIApp, config: LoggingMiddlewareConfig) None#

Initialize LoggingMiddleware.

Parameters:
  • app – The next ASGI app to call.

  • config – An instance of LoggingMiddlewareConfig.

async log_request(scope: Scope, receive: Receive) None#

Extract request data and log the message.

Parameters:
  • scope – The ASGI connection scope.

  • receive – ASGI receive callable

Returns:

None

log_response(scope: Scope) None#

Extract the response data and log the message.

Parameters:

scope – The ASGI connection scope.

Returns:

None

log_message(values: dict[str, Any]) None#

Log a message.

Parameters:

values – Extract values to log.

Returns:

None

async extract_request_data(request: Request) dict[str, Any]#

Create a dictionary of values for the message.

Parameters:

request – A Request instance.

Returns:

An dict.

extract_response_data(scope: Scope) dict[str, Any]#

Extract data from the response.

Parameters:

scope – The ASGI connection scope.

Returns:

An dict.

create_send_wrapper(scope: Scope, send: Send) Send#

Create a send wrapper, which handles logging response data.

Parameters:
  • scope – The ASGI connection scope.

  • send – The ASGI send function.

Returns:

An ASGI send function.

class litestar.middleware.logging.LoggingMiddlewareConfig#

Bases: object

Configuration for LoggingMiddleware

exclude: str | list[str] | None = None#

List of paths to exclude from logging.

exclude_opt_key: str | None = None#

An identifier to use on routes to disable logging for a particular route.

include_compressed_body: bool = False#

Include body of compressed response in middleware. If “body” not set in. response_log_fields this config value is ignored.

logger_name: str = 'litestar'#

Name of the logger to retrieve using app.get_logger(“<name>”).

request_cookies_to_obfuscate: set[str]#

Request cookie keys to obfuscate.

Obfuscated values are replaced with ‘*’.

request_headers_to_obfuscate: set[str]#

Request header keys to obfuscate.

Obfuscated values are replaced with ‘*’.

response_cookies_to_obfuscate: set[str]#

Response cookie keys to obfuscate.

Obfuscated values are replaced with ‘*’.

__init__(exclude: str | list[str] | None = None, exclude_opt_key: str | None = None, include_compressed_body: bool = False, logger_name: str = 'litestar', request_cookies_to_obfuscate: set[str] = <factory>, request_headers_to_obfuscate: set[str] = <factory>, response_cookies_to_obfuscate: set[str] = <factory>, response_headers_to_obfuscate: set[str] = <factory>, request_log_message: str = 'HTTP Request', response_log_message: str = 'HTTP Response', request_log_fields: ~typing.Iterable[~typing.Literal['path', 'method', 'content_type', 'headers', 'cookies', 'query', 'path_params', 'body', 'scheme', 'client']] = ('path', 'method', 'content_type', 'headers', 'cookies', 'query', 'path_params', 'body'), response_log_fields: ~typing.Iterable[~typing.Literal['status_code', 'headers', 'body', 'cookies']] = ('status_code', 'cookies', 'headers', 'body'), middleware_class: type[litestar.middleware.logging.LoggingMiddleware] = <class 'litestar.middleware.logging.LoggingMiddleware'>) None#
response_headers_to_obfuscate: set[str]#

Response header keys to obfuscate.

Obfuscated values are replaced with ‘*’.

request_log_message: str = 'HTTP Request'#

Log message to prepend when logging a request.

response_log_message: str = 'HTTP Response'#

Log message to prepend when logging a response.

request_log_fields: Iterable[Literal['path', 'method', 'content_type', 'headers', 'cookies', 'query', 'path_params', 'body', 'scheme', 'client']] = ('path', 'method', 'content_type', 'headers', 'cookies', 'query', 'path_params', 'body')#

Fields to extract and log from the request.

Notes

  • The order of fields in the iterable determines the order of the log message logged out.

    Thus, re-arranging the log-message is as simple as changing the iterable.

  • To turn off logging of requests, use and empty iterable.

response_log_fields: Iterable[Literal['status_code', 'headers', 'body', 'cookies']] = ('status_code', 'cookies', 'headers', 'body')#

Fields to extract and log from the response. The order of fields in the iterable determines the order of the log message logged out.

Notes

  • The order of fields in the iterable determines the order of the log message logged out.

    Thus, re-arranging the log-message is as simple as changing the iterable.

  • To turn off logging of responses, use and empty iterable.

middleware_class#

Middleware class to use.

Should be a subclass of [litestar.middleware.LoggingMiddleware].

alias of LoggingMiddleware

__post_init__() None#

Override default Pydantic type conversion for iterables.

Parameters:

value – An iterable

Returns:

The value argument cast as a tuple.

property middleware: DefineMiddleware#

Use this property to insert the config into a middleware list on one of the application layers.

Examples

from litestar import Litestar, Request, get
from litestar.logging import LoggingConfig
from litestar.middleware.logging import LoggingMiddlewareConfig

logging_config = LoggingConfig()

logging_middleware_config = LoggingMiddlewareConfig()


@get("/")
def my_handler(request: Request) -> None: ...


app = Litestar(
    route_handlers=[my_handler],
    logging_config=logging_config,
    middleware=[logging_middleware_config.middleware],
)
Returns:

An instance of DefineMiddleware including self as the config kwarg value.