Skip to content

[BE-49] Structured logging with Pino #1239

Description

@yusuftomilola

Overview

The platform uses NestJS's default console logger which outputs unstructured text. In production this is unqueryable by log aggregators (Datadog, CloudWatch, Papertrail). Structured JSON logging with Pino gives every log entry a consistent schema with correlation IDs for request tracing.

Context

  • backend/src/main.ts — where app.useLogger() is configured
  • NestJS supports custom loggers via LoggerModule from nestjs-pino
  • pino-http middleware provides HTTP request/response logs

Tasks

  • Install packages: npm install nestjs-pino pino-http pino-pretty
  • In backend/src/app.module.ts: add LoggerModule.forRoot({ pinoHttp: { ... } }) from nestjs-pino
  • Log format configuration:
    • NODE_ENV=production: plain JSON (no pino-pretty)
    • NODE_ENV=development: human-readable via pino-pretty (transport: { target: 'pino-pretty' })
  • Each log entry includes: timestamp (ISO 8601), level, context (NestJS module/class name), msg, reqId (auto-generated UUID per request via genReqId)
  • HTTP request/response logs via pino-http: log method, url, statusCode, responseTime on every response
  • Redact sensitive fields using Pino's redact option: ['req.headers.authorization', '*.password', '*.totpSecret', '*.smsOtpCode']
  • In backend/src/main.ts: call app.useLogger(app.get(Logger)) from nestjs-pino to replace NestJS default logger

Files to Modify / Create

  • backend/src/main.ts
  • backend/src/app.module.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions