Successfully replaced console.log statements with Winston structured logging for production-ready monitoring and debugging.
npm install winston winston-daily-rotate-file- 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)
- 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
- Comprehensive documentation
- Usage examples
- Best practices
- Migration guide from console.log
- Imported logger and requestLogger middleware
- Replaced
console.logwithlogger.info - Replaced
console.errorwithlogger.error(with structured metadata) - Added request logging middleware to Express pipeline
- Added logger usage examples
- Demonstrates proper structured logging with metadata
- Added
logs/directory to prevent committing log files
✅ 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
- 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)
Located in logs/ directory:
error-YYYY-MM-DD.log: Error-level logs onlycombined-YYYY-MM-DD.log: All log levels
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 });Build verification completed successfully:
npm run build:server-
Run the server to test logging in development:
npm run dev:server
-
Test in production mode:
NODE_ENV=production npm run start:server
-
Monitor logs in the
logs/directory when running in production -
Consider integrating with log aggregation services (e.g., ELK Stack, Datadog, CloudWatch)
- 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)