Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Sep 22, 2023
1 parent c16c40f commit 3367764
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/core/fastapi/middleware/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@

class LoggingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Start time
start_time = time.time()

# Process request
response = await call_next(request)

# End time
end_time = time.time()

# Calculate request processing time
process_time = end_time - start_time

log_info = {
"Request Method": request.method,
"Request URL": str(request.url),
"Response Status Code": response.status_code,
"Processing Time": f"{process_time:.4f}s",
}
# Log the request
logger.info(log_info)

process_time = time.time() - start_time

query_params_list = [
(key, value if key != "token" else "***")
for key, value in request.query_params.items()
]

logger.debug(
{
"url": request.url.path,
"params": query_params_list,
"process_time": f"{process_time:.4f}",
}
)
return response

0 comments on commit 3367764

Please sign in to comment.