logging#

class starlite.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 starlite.middleware.logging.LoggingMiddlewareConfig#

Bases: BaseModel

Configuration for LoggingMiddleware

exclude: Optional[Union[str, List[str]]]#

List of paths to exclude from logging.

exclude_opt_key: Optional[str]#

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

include_compressed_body: bool#

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

logger_name: str#

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 ‘*’.

response_headers_to_obfuscate: Set[str]#

Response header keys to obfuscate.

Obfuscated values are replaced with ‘*’.

request_log_message: str#

Log message to prepend when logging a request.

response_log_message: str#

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']]#

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']]#

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: Type[LoggingMiddleware]#

Middleware class to use.

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

property middleware: DefineMiddleware#

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

Examples

Returns:

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