Skip to content

feat: add onRequest logging hook to capture method and URL per request#66

Open
SdSarthak wants to merge 2 commits into
Dev-Card:mainfrom
SdSarthak:feat/request-logging
Open

feat: add onRequest logging hook to capture method and URL per request#66
SdSarthak wants to merge 2 commits into
Dev-Card:mainfrom
SdSarthak:feat/request-logging

Conversation

@SdSarthak
Copy link
Copy Markdown

Summary

  • Adds app.addHook('onRequest', ...) in apps/backend/src/app.ts that logs each incoming request's method and url via pino (app.log.info)
  • The hook fires before any route handler, so every request — including health checks, auth, and API routes — is logged
  • Uses the existing pino logger already configured in Fastify, so log format (pino-pretty in dev, JSON in production) is consistent with the rest of the app
  • Adds apps/backend/src/__tests__/app.test.ts with two vitest tests:
    • Asserts the log entry with { method, url } is written for an injected GET /health request
    • Asserts the health endpoint still returns 200 with { status: 'ok' } (no regression)

Closes #8

Test plan

  • cd apps/backend && npm test — both new tests pass
  • Start the server and make any request — log line appears with method + url
  • Existing tests continue to pass

- Add app.addHook('onRequest') in app.ts that logs request.method and
  request.url via pino (app.log.info) for every incoming request
- Add apps/backend/src/__tests__/app.test.ts with two vitest tests:
  one asserting the log entry is created for GET /health, one asserting
  existing health check behavior is unchanged (200 + status ok)

Closes Dev-Card#8
Copilot AI review requested due to automatic review settings May 10, 2026 18:25
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Fastify onRequest hook to log each incoming request’s method and URL, and introduces a small Vitest suite to verify the hook runs and /health remains unchanged.

Changes:

  • Added an onRequest hook in buildApp() that logs { method, url } for every request.
  • Added Vitest tests to assert the log entry is emitted and the health endpoint still returns { status: 'ok' }.
  • (Unrelated) Introduced an nfcRoutes import in app.ts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
apps/backend/src/app.ts Adds onRequest logging hook; also adds an nfcRoutes import.
apps/backend/src/tests/app.test.ts Adds tests for request logging and /health response.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/backend/src/app.ts
import { followRoutes } from './routes/follow.js';
import { connectRoutes } from './routes/connect.js';
import { analyticsRoutes } from './routes/analytics.js';
import { nfcRoutes } from './routes/nfc.js';
Comment thread apps/backend/src/app.ts Outdated

// ─── Request Logger ───
app.addHook('onRequest', async (request) => {
app.log.info({ method: request.method, url: request.url }, 'incoming request');
Comment on lines +7 to +23
const logSpy = vi.spyOn(app.log, 'info');

await app.inject({ method: 'GET', url: '/health' });

const calls = logSpy.mock.calls.map((c) => c[0]);
const loggedRequest = calls.find(
(c: any) => typeof c === 'object' && c?.method === 'GET' && c?.url === '/health'
);
expect(loggedRequest).toBeDefined();
});

it('returns 200 for health check (existing behavior unchanged)', async () => {
const app = await buildApp();
const res = await app.inject({ method: 'GET', url: '/health' });
expect(res.statusCode).toBe(200);
const body = JSON.parse(res.body);
expect(body.status).toBe('ok');
Comment thread apps/backend/src/__tests__/app.test.ts Outdated
Comment on lines +6 to +9
const app = await buildApp();
const logSpy = vi.spyOn(app.log, 'info');

await app.inject({ method: 'GET', url: '/health' });
@SdSarthak
Copy link
Copy Markdown
Author

Fixed in the follow-up commit:

  • The onRequest hook now strips the query string before logging (request.url.split('?')[0]) so tokens or sensitive params in query strings are never written to logs.
  • Added afterEach(async () => { await app?.close(); }) in the test suite so Fastify shuts down cleanly after each test — handles the leaked handles concern.
  • The nfcRoutes import on this branch was intentional (the NFC endpoint PR was branched from here); it's addressed in PR feat: add GET /api/nfc/payload endpoint for NFC tag payload generation #67.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backend: add request logging middleware for endpoints

2 participants