Skip to content

feat: Added request logging middleware to the backend#489

Open
yughpatel wants to merge 3 commits into
param20h:devfrom
yughpatel:dev
Open

feat: Added request logging middleware to the backend#489
yughpatel wants to merge 3 commits into
param20h:devfrom
yughpatel:dev

Conversation

@yughpatel
Copy link
Copy Markdown

📋 PR Checklist

Thank you for contributing to PDF-Assistant-RAG! 🎉
Please fill out this template before submitting. PRs without it filled in will be closed.


🔗 Related Issue

Closes #36

📝 What does this PR do?

This PR implements custom structured JSON logging using loguru for the backend, replacing generic console logging with meaningful, context-rich logging tailored to RAG workflows:

  1. Structured Logging Middleware: Implements StructuredLoggingMiddleware to capture core HTTP metrics (method, path, status code, response time in ms), unique request IDs (X-Request-ID), and authenticated user IDs.
  2. Context-Aware Fields: Automatically binds application-specific context variables across the request lifecycle:
    • Uploads: Logs the filename and file_size on document uploads (/upload and /urlupload).
    • Chat Queries: Logs the query text and chunks_retrieved count from the vector database on RAG chat queries and streams (/chat/ask and /chat/ask/stream).
  3. Standard Log Redirection: Intercepts standard python logging outputs (including FastAPI, Uvicorn, and SQLAlchemy) and redirects them through loguru to maintain a single JSON log schema.
  4. Rotation and Levels: Configures Loguru with file-based rotation (10 MB), retention (10 days), and compression (zip) written to ./data/logs/app.log, loading environment-based log levels (LOG_LEVEL=DEBUG in development, INFO in production).
  5. Documentation: Overwrote CONTRIBUTING.md with guidelines on branch rules, development setup, and hook rules.

🗂️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🔧 Refactor / code cleanup
  • 📝 Documentation update
  • 🎨 UI / styling change
  • ⚙️ CI / tooling / config change
  • 🧪 Tests

🧪 How was this tested?

  • Ran the backend locally (uvicorn app.main:app --reload)
  • Tested the affected API endpoints manually
    Details:
  • Ran a local uvicorn server in a clean Python 3.14 environment.
  • Executed end-to-end integration tests (user registration, login, document upload, RAG ask, and RAG streaming) verifying that requests correctly write clean, single-line JSON log outputs containing all request-local context parameters to console stdout and backend/data/logs/app.log.

📸 Screenshots (if UI change)

N/A

⚠️ Anything to flag for reviewers?

  • Added loguru as a new backend dependency in requirements.txt.
  • Local contextvars were combined with FastAPI request.state to ensure robust parameter propagation back to the middleware logs across different asyncio task environments.

✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@yughpatel yughpatel requested a review from param20h as a code owner June 5, 2026 22:08
@param20h param20h changed the title feat: Added request logging middleware to the backend #36 feat: Added request logging middleware to the backend Jun 7, 2026
@param20h param20h added gssoc GirlScript Summer of Code 2026 issue/PR gssoc:approved Approved for GSSoC base points (+50 pts) level:intermediate +35 pts quality:clean 1.2x multiplier type:feature +10 pts mentor:param20h Mentor for this PR labels Jun 7, 2026
@param20h
Copy link
Copy Markdown
Owner

param20h commented Jun 7, 2026

resolve conflicts
@yughpatel

@yughpatel
Copy link
Copy Markdown
Author

resolve conflicts
@yughpatel

Will work on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved for GSSoC base points (+50 pts) gssoc GirlScript Summer of Code 2026 issue/PR level:intermediate +35 pts mentor:param20h Mentor for this PR quality:clean 1.2x multiplier type:feature +10 pts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add request logging middleware to the backend

2 participants