Skip to content

[Backend] Implement Structured JSON Logging with Log Levels #110

Description

@devJaja

Problem

Current logging in backend/src/api/middleware/logger.ts uses plain console.log with a basic timestamp format. This is not machine-parseable, has no log levels, and makes log aggregation (ELK, Datadog, etc.) impossible.

Current State

// logger.ts output format:
// [2024-01-01T00:00:00.000Z] GET /api/tasks 200 45ms

Proposed Solution

  1. Add pino as the logging library (fast, structured by default)
  2. Create backend/src/logger.ts with configurable levels:
    • LOG_LEVEL env var (debug/info/warn/error)
    • Development: pretty-printed
    • Production: JSON output
  3. Create child loggers per module:
    • logger.child({ module: 'coordinator' })
    • logger.child({ module: 'api', correlationId })
  4. Replace all console.log/console.error with structured logger
  5. Redact sensitive fields (API keys, tokens) automatically

Files to Modify

  • backend/src/api/middleware/logger.ts
  • backend/src/coordinator/index.ts
  • backend/src/db/index.ts
  • backend/src/registry/sync.ts
  • backend/src/server.ts
  • New: backend/src/logger.ts

Acceptance Criteria

  • Pino logger configured with JSON output
  • LOG_LEVEL env var respected
  • All console.log replaced with structured logger
  • Correlation IDs included in all log lines
  • Sensitive fields redacted in logs
  • Pretty-print in development mode
  • Performance: no measurable latency increase

Difficulty

High — touches every file that logs

Labels

backend services enhancement

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions