feat: include query parameters in request logs#797
Conversation
Update LoggingMiddleware to log the full request path including query parameters, matching uvicorn's console output format. This provides critical information for debugging and detecting abuse patterns. Example log output: 127.0.0.1 - "GET /api/users?page=2&limit=10" 200 (0.045s)
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
Add two unit tests to verify query parameters are correctly logged: - test_middleware_logs_query_parameters: ensures query params are included - test_middleware_logs_without_query_parameters: ensures clean output without trailing ? Coverage for logging_middleware.py now at 100%.
There was a problem hiding this comment.
Pull request overview
This PR enhances the logging middleware to include query parameters in request logs, matching uvicorn's console output format. This improvement helps with debugging and detecting abuse patterns by providing complete request information in log files.
Key changes:
- Modified
LoggingMiddleware.dispatch()to append query strings to the path when present - Added comprehensive test coverage for both query parameter and non-query parameter scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| app/middleware/logging_middleware.py | Enhanced logging to include query parameters in the logged request path, maintaining backward compatibility for requests without query strings |
| tests/unit/test_logging.py | Added two new test cases covering query parameter inclusion and proper handling of requests without query parameters |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Remove unnecessary str() conversion on request.url.path as it already returns a string type in Starlette.
cbeabf2 to
48e7c98
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
Problem
The loguru file logging was only capturing the path (
/api/users) without query parameters, unlike uvicorn's console output. This made it difficult to debug issues and detect abuse patterns.Before:
After:
Changes
Modified
LoggingMiddlewareinapp/middleware/logging_middleware.pyto append query string to path when present. Maintains backward compatibility - requests without query params are logged unchanged.🤖 Generated with Claude Code