Skip to content

Latest commit

 

History

History
125 lines (94 loc) · 3.48 KB

File metadata and controls

125 lines (94 loc) · 3.48 KB

Winston Logging Implementation Summary

Overview

Successfully replaced console.log statements with Winston structured logging for production-ready monitoring and debugging.

Changes Made

1. Dependencies Installed

npm install winston winston-daily-rotate-file

2. Files Created

server/config/logger.ts

  • Centralized logger configuration
  • Environment-specific formatting:
    • Development: Colored, human-readable console output with timestamps
    • Production: JSON format for log aggregation tools
  • Log levels: error, warn, info, debug
  • Daily log rotation for production (14-day retention, 20MB max file size)

server/middleware/requestLogger.ts

  • Express middleware for automatic HTTP request logging
  • Captures:
    • Request method, URL, IP, user agent
    • Response status code
    • Request duration in milliseconds
  • Logs errors (4xx/5xx) at error level, success at info level

server/LOGGING.md

  • Comprehensive documentation
  • Usage examples
  • Best practices
  • Migration guide from console.log

3. Files Updated

server/index.ts

  • Imported logger and requestLogger middleware
  • Replaced console.log with logger.info
  • Replaced console.error with logger.error (with structured metadata)
  • Added request logging middleware to Express pipeline

server/routes/example.ts

  • Added logger usage examples
  • Demonstrates proper structured logging with metadata

.gitignore

  • Added logs/ directory to prevent committing log files

Features Implemented

✅ Structured logging with metadata
✅ Environment-specific log formatting
✅ Multiple log levels (error, warn, info, debug)
✅ Automatic HTTP request logging with timing
✅ Daily log rotation in production
✅ Error logging with stack traces
✅ Console output in development
✅ JSON logs for production monitoring

Log Levels

  • error: Application errors and exceptions
  • warn: Warning messages for potentially harmful situations
  • info: General informational messages (default in production)
  • debug: Detailed debugging information (default in development)

Production Log Files

Located in logs/ directory:

  • error-YYYY-MM-DD.log: Error-level logs only
  • combined-YYYY-MM-DD.log: All log levels

Usage Example

import logger from './config/logger';

// Basic logging
logger.info('User action completed', { userId: '123', action: 'login' });
logger.error('Database error', { error: err.message, stack: err.stack });
logger.warn('Rate limit approaching', { userId: '123', requests: 95 });
logger.debug('Processing data', { dataSize: data.length });

Testing

Build verification completed successfully:

npm run build:server

Next Steps

  1. Run the server to test logging in development:

    npm run dev:server
  2. Test in production mode:

    NODE_ENV=production npm run start:server
  3. Monitor logs in the logs/ directory when running in production

  4. Consider integrating with log aggregation services (e.g., ELK Stack, Datadog, CloudWatch)

Migration Checklist

  • Install Winston and winston-daily-rotate-file
  • Create logger configuration
  • Create request logging middleware
  • Update server/index.ts to use logger
  • Replace console.log statements
  • Add logs/ to .gitignore
  • Create documentation
  • Verify build succeeds
  • Test in development environment
  • Test in production environment
  • Configure log monitoring/alerting (optional)