Skip to content

Replace console.log with structured logging using Winston or Pino #161

Description

@Josie123-Dev

What

Replace all console.log and console.error calls in the backend with structured logging using Pino (or Winston). Add log levels, JSON output format, request context (request ID, user ID), and correlation IDs for tracing requests across services.

Why

The backend currently uses console.log for logging (visible in main.ts and likely scattered across services). For a health credential platform in production, this is insufficient:

  • No log levels (can't filter noise from critical errors)
  • No structured format (can't query logs in CloudWatch/Datadog)
  • No request correlation (can't trace a single request through multiple services)
  • No sensitive data filtering (health data could leak into logs)

SECURITY.md mentions Sentry and Datadog integration — structured logging is the prerequisite for both.

Scope

In scope:

  • Install nestjs-pino (or nest-winston) and configure as the NestJS logger
  • Replace all console.log/console.error calls with proper logger methods
  • Add request logging middleware (method, path, status, duration, request ID)
  • Add log levels: debug, info, warn, error
  • JSON format in production, pretty-print in development
  • Add LOG_LEVEL env var to .env.example

Out of scope:

  • Shipping logs to external services (Datadog, CloudWatch)
  • Log rotation configuration
  • Audit logging (already exists in backend/src/audit/)
  • Frontend logging

Acceptance Criteria

  • nestjs-pino (or nest-winston) is installed and configured
  • All console.log and console.error calls in backend/src/ are replaced
  • HTTP requests are logged with: method, path, status code, duration, request ID
  • Log output is JSON in production, pretty-printed in development
  • LOG_LEVEL env var controls minimum log level
  • Sensitive fields (passwords, tokens, health data) are redacted from logs
  • npm run start:dev shows colored, readable logs
  • npm run start:prod outputs JSON logs

Technical Context

  • Main bootstrap: backend/src/main.ts — replace Logger usage
  • nestjs-pino is the most popular NestJS logging integration: https://github.com/iamolegga/nestjs-pino
  • Request context: use pino-http middleware for automatic request logging
  • Correlation IDs: use nestjs-pino's req.id or X-Request-Id header
  • Redaction: use pino's redact option for sensitive fields

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third CampaignbackendBackend related issuesdifficulty:hardHard difficulty issuesobservabilityLogging, monitoring, tracingpriority:highHigh priority issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions