Skip to content

backend: add onRequest request-logging middleware with tests#141

Closed
dangzitou wants to merge 1 commit into
Dev-Card:mainfrom
dangzitou:openmeta/agent-8-backend-add-request-logging-midd-1779062768310
Closed

backend: add onRequest request-logging middleware with tests#141
dangzitou wants to merge 1 commit into
Dev-Card:mainfrom
dangzitou:openmeta/agent-8-backend-add-request-logging-midd-1779062768310

Conversation

@dangzitou
Copy link
Copy Markdown

Summary

The backend has no request-level logging, making it difficult to trace incoming traffic during development and debugging. This PR adds an onRequest Fastify hook in buildApp() that logs each request's method and URL via the built-in pino logger (app.log.info) with a structured object, keeping output consistent with existing pino/pino-pretty formatting. A new Vitest test suite in app.test.ts verifies the hook fires by spying on app.log.info after injecting a synthetic request.

Changes

  • Register an async onRequest hook in apps/backend/src/app.ts that calls app.log.info({ method: request.method, url: request.url }, 'incoming request') after the Fastify instance is created and before plugin/route registration.
  • Create apps/backend/src/tests/app.test.ts with a Vitest suite that imports buildApp, mocks heavy plugins (prisma, redis, fastifyStatic) for lightweight testing, spies on app.log.info, injects a GET /health request via app.inject(), and asserts the spy received the expected method, url, and message.
  • No changes to existing routes, plugins, or middleware; the hook is additive only.

Validation

  • pnpm run test — new app.test.ts suite and all existing suites (follow, profiles, connect) should pass. Command detected in repo but not yet executed; pending CI run.
  • pnpm run lint — no new lint errors expected since the hook and test follow existing project conventions. Command detected in repo but not yet executed; pending CI run.
  • Manual smoke test: start dev server, curl http://localhost:3000/health, verify structured log line appears in terminal output (pending).

Risks

  • The onRequest hook fires before authentication and route handling, so health-check, static-asset, and favicon requests will all be logged. In production this could increase log volume if static files are served from the same Fastify process.
  • Mocking prismaPlugin, redisPlugin, and fastifyStatic in the test requires matching the exact import paths used in app.ts. If module resolution differs between test and production environments the test will fail at buildApp() invocation.
  • No automated tests have been executed yet; all validation is pending pnpm run test and pnpm run lint in CI.

@Harxhit
Copy link
Copy Markdown
Collaborator

Harxhit commented May 18, 2026

@dangzitou Closing this PR because #142 duplicates the work already covered in this PR.

@Harxhit Harxhit closed this May 18, 2026
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.

2 participants