Implementation of Winston with daily file rotation for logging in Node.js applications, featuring multi-level logging, daily rotation, and comprehensive request tracking.
- Three-tier logging system:
- Access logs (L1) — Basic request/response information
- Detail logs (L2) — Comprehensive debug information
- Error logs (L3) — Error tracking with stack traces
- Daily log rotation with 200MB file size limit, 14-day retention, and automatic compression
- Request Tracking — Method, URL, response time, status code, and IP address logging
- Error Handling — Stack trace capture, request context preservation, and detailed error metadata
- Security — Automatic directory creation, safe file rotation, and error protection
npm install winston winston-daily-rotate-file node-cronconst logger = require('./path/to/logger');
const express = require('express');
const app = express();
app.use(logger.requestLogger); // Apply logger middleware
app.use(logger.errorHandler); // Error handling| Level | Code | Description |
|---|---|---|
| Emergency | 0 | System is unusable |
| Alert | 1 | Immediate action required |
| Critical | 2 | Critical conditions |
| Error | 3 | Error conditions |
| Warning | 4 | Warning conditions |
| Notice | 5 | Normal but significant condition |
| Info | 6 | Informational messages |
| Debug | 7 | Debug-level messages |
logs/
├── access/
│ └── DD-MM-YYYY-access.log
├── detail/
│ └── DD-MM-YYYY-detail.log
└── error/
└── DD-MM-YYYY-error.log
Customize logging behavior by modifying the file rotation schedule (cron expression), file size limits, retention period, timestamp format, and log formats.
Built and maintained by Anusthan Singh · © 2025