@@ -302,8 +302,8 @@ async def __call__(self, scope, receive, send):
302302 request_logger = self .logger .bind (** context )
303303
304304 # Log request
305- request_logger .info (
306- f"{ scope ['method' ]} { scope ['path' ]} " , extra = { "event" : "http.request.start" }
305+ request_logger .bind ( event = "http.request.start" ). info (
306+ f"{ scope ['method' ]} { scope ['path' ]} "
307307 )
308308
309309 start_time = time .time ()
@@ -319,38 +319,27 @@ async def send_wrapper(message):
319319 await self .app (scope , receive , send_wrapper )
320320 except Exception as e :
321321 duration_ms = (time .time () - start_time ) * 1000
322- request_logger .error (
323- f"Request failed: { type (e ).__name__ } " ,
324- extra = {
325- "event" : "http.error" ,
326- "status" : 500 ,
327- "duration_ms" : duration_ms ,
328- },
329- )
322+ request_logger .bind (
323+ event = "http.error" , status = 500 , duration_ms = duration_ms
324+ ).error (f"Request failed: { type (e ).__name__ } " )
330325 raise
331326 else :
332327 duration_ms = (time .time () - start_time ) * 1000
333328
334329 # Check for slow request
335330 if duration_ms > self .slow_request_threshold_ms :
336- request_logger .warning (
337- f"Slow request: { scope ['method' ]} { scope ['path' ]} " ,
338- extra = {
339- "event" : "http.slow_request" ,
340- "duration_ms" : duration_ms ,
341- "threshold_ms" : self .slow_request_threshold_ms ,
342- "status" : status_code ,
343- },
344- )
331+ request_logger .bind (
332+ event = "http.slow_request" ,
333+ duration_ms = duration_ms ,
334+ threshold_ms = self .slow_request_threshold_ms ,
335+ status = status_code ,
336+ ).warning (f"Slow request: { scope ['method' ]} { scope ['path' ]} " )
345337 else :
346- request_logger .info (
347- f"{ scope ['method' ]} { scope ['path' ]} { status_code } " ,
348- extra = {
349- "event" : "http.response.complete" ,
350- "status" : status_code ,
351- "duration_ms" : duration_ms ,
352- },
353- )
338+ request_logger .bind (
339+ event = "http.response.complete" ,
340+ status = status_code ,
341+ duration_ms = duration_ms ,
342+ ).info (f"{ scope ['method' ]} { scope ['path' ]} { status_code } " )
354343
355344 finally :
356345 TraceContextManager .clear_context ()
0 commit comments