Skip to content

feat(server): introduce structured logging (levels, request IDs, JSON) - #273

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
Pallavikumarimdb:feat/introduce-structured-logging-in-server
Jun 18, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
Pallavikumarimdb:feat/introduce-structured-logging-in-server

Conversation

@Pallavikumarimdb

Copy link
Copy Markdown
Contributor

Summary

Phase 1 of structured logging infrastructure for the server #245.

Changes:

  • Added pino as a dependency for fast, JSON-by-default structured logging
  • Created server/src/lib/logger.js — centralized logger instance with configurable LOG_LEVEL (defaults to debug in development, info in production) and redaction of sensitive headers
  • Created server/src/middleware/request-id.js — attaches a UUID correlation ID to each request (req.id), sets x-request-id response header, and provides a per-request child logger (req.log) with the request ID bound
  • Updated server/src/server.js to integrate the request ID middleware and replace key console.* calls with structured logger calls at appropriate levels:
    • Per-request access log moved to debug level (reduces noise; off by default in production)
    • CORS configuration, socket connect/disconnect, and startup messages routed through the logger

Env:

  • LOG_LEVEL added to .env.example (e.g. info in production, debug locally)

Note: This covers Phase 1 (core infrastructure). Remaining console.* calls across the codebase will be migrated incrementally in follow-up PRs.

Related issue

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

@Pallavikumarimdb
Pallavikumarimdb marked this pull request as ready for review June 17, 2026 12:19

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Pallavikumarimdb — really clean Phase 1 (pino + LOG_LEVEL, request-id correlation, redaction, JSON). One blocking issue before merge:

server/src/middleware/request-id.js — wrong logger import path.

import logger from '../../lib/logger.js';

The file is at src/middleware/, so ../../lib resolves to server/lib/logger.js (no such dir) — the logger lives at src/lib/logger.js. At startup server.js imports this middleware → ERR_MODULE_NOT_FOUND → the server won't boot. (CI didn't catch it because the server job only runs eslint/prettier/vitest, which don't resolve ESM paths to disk, and there's no startup smoke test.)

Fix is one character — use a single ../ (like middleware/index.js does with ../config/...):

import logger from '../lib/logger.js';

Everything else looks good to merge once that's fixed. 🙌

Minor (non-blocking, optional): the redact paths target req.headers.* but the request log currently passes { method, path, origin, ip } rather than the req object, so redaction only kicks in once req itself is logged — fine to leave for a later phase.

@Pallavikumarimdb

Copy link
Copy Markdown
Contributor Author

Thanks @Pallavikumarimdb — really clean Phase 1 (pino + LOG_LEVEL, request-id correlation, redaction, JSON). One blocking issue before merge:

server/src/middleware/request-id.js — wrong logger import path.

import logger from '../../lib/logger.js';

The file is at src/middleware/, so ../../lib resolves to server/lib/logger.js (no such dir) — the logger lives at src/lib/logger.js. At startup server.js imports this middleware → ERR_MODULE_NOT_FOUND → the server won't boot. (CI didn't catch it because the server job only runs eslint/prettier/vitest, which don't resolve ESM paths to disk, and there's no startup smoke test.)

Fix is one character — use a single ../ (like middleware/index.js does with ../config/...):

import logger from '../lib/logger.js';

Everything else looks good to merge once that's fixed. 🙌

Minor (non-blocking, optional): the redact paths target req.headers.* but the request log currently passes { method, path, origin, ip } rather than the req object, so redaction only kicks in once req itself is logged — fine to leave for a later phase.

@gkhngyk I have made the suggested changes, could you please review again?

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Import path fixed (../lib/logger.js) — server boots cleanly now. Structured logging Phase 1 looks good: pino, LOG_LEVEL, request-id correlation, redaction, JSON. Thanks @Pallavikumarimdb! 🙌

@gkhngyk
gkhngyk merged commit 386e827 into ansvisor:main Jun 18, 2026
2 checks passed
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