Skip to content

feat: add request logging middleware for backend endpoints#118

Open
algojogacor wants to merge 1 commit into
Dev-Card:mainfrom
algojogacor:feat/request-logging-middleware
Open

feat: add request logging middleware for backend endpoints#118
algojogacor wants to merge 1 commit into
Dev-Card:mainfrom
algojogacor:feat/request-logging-middleware

Conversation

@algojogacor
Copy link
Copy Markdown

Summary

Adds a lightweight onRequest hook to the Fastify backend that logs every incoming request's HTTP method and URL via the existing pino logger infrastructure.

Solution

Added app.addHook('onRequest', ...) in buildApp() that logs { method, url } fields using request.log.info(). This integrates with the existing pino logger configuration and outputs structured log entries for each request.

Changes

  • apps/backend/src/app.ts: Added onRequest hook after database/cache plugin registration and before auth decorator. Logs request.method and request.url.
  • apps/backend/src/__tests__/app.test.ts (new): Tests that the hook fires on GET and POST requests by spying on app.log.info and verifying the logged fields.

Testing

  • Test file created with two test cases:
    • Verifies GET /health produces log entry with { method: 'GET', url: '/health' }
    • Verifies POST /api/u/testuser produces log entry with { method: 'POST', url: '/api/u/testuser' }
  • Tests use Fastify's inject() method for lightweight HTTP testing
  • Tests pass when run with a configured database (Prisma requires database connection)

@ShantKhatri
Copy link
Copy Markdown
Contributor

@algojogacor Thanks for the PR. This is not yet discussed, and I'm not to understand the change here. could you please add detailed explaination here, happy to look into this.

@Harxhit Harxhit added the gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. label May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants