Smart Logging

Production-ready logging system with multiple transports, correlation IDs, and performance tracking.

Overview

Log Types

1. Request Logs

2. Error Logs

3. Performance Logs

4. Security Logs

Transports

Console Transport

  • Development: Pretty-printed, colored
  • Production: JSON format

File Transport

Output format: JSON lines (one entry per line)

MongoDB Transport

Indexes for fast queries:
  • timestamp (descending)
  • correlationId
  • type
  • level
TTL: 30 days (automatic expiration)

Usage

Basic Logging

Request Logging

Automatic via middleware — no manual logging needed:

Error Logging

Throw errors — middleware handles logging:

Performance Tracking

Security Events

Configuration

Correlation ID

Every request gets a correlation ID for end-to-end tracking:
Passed via:
  • Request: X-Correlation-ID header
  • Response: Same header echoed back
  • Logs: correlationId field

Querying Logs

MongoDB

File

Best Practices

DO ✅

  • Use correlation IDs for request tracking
  • Log at appropriate levels (info for actions, warn for warnings, error for failures)
  • Include relevant context in log entries
  • Use structured logging (objects, not strings)

DON’T ❌

  • Log sensitive data (passwords, tokens, credit cards)
  • Log in tight loops (causes log flooding)
  • Use console.log instead of the logger
  • Log without context