logging#
- class litestar.middleware.logging.LoggingMiddleware#
Bases:
AbstractMiddleware
Logging middleware.
- __init__(app: ASGIApp, config: LoggingMiddlewareConfig) None #
Initialize
LoggingMiddleware
.
- 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.
- class litestar.middleware.logging.LoggingMiddlewareConfig#
Bases:
object
Configuration for
LoggingMiddleware
- 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.
- 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_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.