logging¶
- class litestar.middleware.logging.LoggingMiddleware¶
-
日志中间件。
- __init__(app: ASGIApp, config: LoggingMiddlewareConfig) None¶
Initialize
LoggingMiddleware.
- async log_request(scope: Scope, receive: Receive) None¶
提取请求数据并记录消息。 Args: scope: ASGI 连接作用域。 receive: ASGI 接收可调用对象 Returns: None
- async extract_request_data(request: Request) dict[str, Any]¶
为消息创建值字典。 Args: request: 一个
Request实例。 Returns: 一个 dict。
- class litestar.middleware.logging.LoggingMiddlewareConfig¶
基类:
objectConfiguration 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_fieldsthis 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: 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: Literal['status_code', 'headers', 'body', 'cookies']]=('status_code', 'cookies', 'headers', 'body'), middleware_class: type[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: Collection[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.
备注
- 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: Collection[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.
备注
- 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].
- __post_init__() None¶
Override default Pydantic type conversion for iterables.
- 参数:
value¶ -- An iterable
- 返回:
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.
示例
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], )
- 返回:
An instance of DefineMiddleware including
selfas the config kwarg value.