88from litestar .status_codes import HTTP_500_INTERNAL_SERVER_ERROR
99
1010from microbootstrap .instruments .logging_instrument import fill_log_message
11+ from .utils import optimize_exclude_paths
1112
1213
1314def build_litestar_logging_middleware (
1415 exclude_endpoints : typing .Iterable [str ],
1516) -> type [MiddlewareProtocol ]:
17+ endpoints_to_ignore : typing .Collection [str ] = optimize_exclude_paths (exclude_endpoints )
18+
1619 class LitestarLoggingMiddleware (MiddlewareProtocol ):
1720 def __init__ (self , app : litestar .types .ASGIApp ) -> None :
1821 self .app = app
@@ -24,14 +27,20 @@ async def __call__(
2427 send_function : litestar .types .Send ,
2528 ) -> None :
2629 request : typing .Final [litestar .Request ] = litestar .Request (request_scope ) # type: ignore[type-arg]
30+
31+ request_path = request .url .path .removesuffix ("/" )
32+
33+ if request_path in endpoints_to_ignore :
34+ await self .app (request_scope , receive , send_function )
35+ return
36+
2737 start_time : typing .Final [int ] = time .perf_counter_ns ()
2838
2939 async def log_message_wrapper (message : litestar .types .Message ) -> None :
30- request_path = request .url .path .removesuffix ("/" )
31- should_log : typing .Final = not any (one_endpoint == request_path for one_endpoint in exclude_endpoints )
32- if message ["type" ] == "http.response.start" and should_log :
33- log_level : str = "info" if message ["status" ] < HTTP_500_INTERNAL_SERVER_ERROR else "exception"
34- fill_log_message (log_level , request , message ["status" ], start_time )
40+ if message ["type" ] == "http.response.start" :
41+ status = message ["status" ]
42+ log_level : str = "info" if status < HTTP_500_INTERNAL_SERVER_ERROR else "exception"
43+ fill_log_message (log_level , request , status , start_time )
3544
3645 await send_function (message )
3746
0 commit comments