Logging Middleware¶
logging_middleware
¶
Middleware that logs each request before and after delegation.
LoggingMiddleware
¶
Bases: Middleware[RequestType, ResponseType]
Logs each request before delegation and the response after.
Responsibilities
- Log the incoming request at debug level.
- Delegate to the next handler unchanged.
- Log the outgoing response at debug level.
Non-Responsibilities
- Transform the request or response.
- Handle errors raised by downstream middleware or the handler.
- Measure execution time — use
TimingMiddlewarefor that. - Sanitize sensitive data — requests and responses are logged
viastr()as-is. If your domain objects carry tokens,
passwords, or PII, wrap this middleware with a sanitizing
decorator or pass aLoggerPortthat performs redaction.
Example
Source code in src/forging_blocks/presentation/builtin/logging_middleware.py
__init__(logger: LoggerPort) -> None
¶
Wrap logger so every request is traced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
LoggerPort
|
A |
required |
process(request: RequestType, next_handler: NextHandler[RequestType, ResponseType]) -> ResponseType
async
¶
Log the request, delegate, then log the response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
RequestType
|
The incoming request. |
required |
next_handler
|
NextHandler[RequestType, ResponseType]
|
The next callable in the pipeline chain. |
required |
Returns:
| Type | Description |
|---|---|
ResponseType
|
The response produced by the downstream handler. |