Course contentsShow
← Node.js
Lesson 1662 of 1,98015. Logging, Observability, and MetricsFree lesson

Building a Log Rotation Strategy

Manage disk space and system stability by implementing file-based log rotation and retention policies.

In this lesson, you will learn how to prevent disk exhaustion and ensure system stability by implementing file-based log rotation and retention policies in a Node.js environment.

As your application matures, writing logs to a single, infinitely growing file is a recipe for disaster. Eventually, the file will consume all available disk space, causing the operating system to crash or preventing your application from writing further logs. Log rotation mitigates this by periodically closing the current log file, archiving it, and starting a fresh one.

The Mechanism of Rotation

Rotation is typically triggered by two factors: size (e.g., when the file hits 100MB) or time (e.g., daily). Once a threshold is met, the system renames the current log file (often appending a timestamp or index) and creates a new app.log. To manage disk space, you must also define a retention policy, which dictates how many archived files to keep or how long to hold them before deletion.

While you could write manual logic using fs.rename and fs.stat, production systems usually delegate this to battle-tested stream management. If you are using a logging library like winston, you can integrate a transport like winston-daily-rotate-file:

const winston = require('winston');
require('winston-daily-rotate-file');

const transport = new (winston.transports.DailyRotateFile)({
  filename: 'logs/app-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  zippedArchive: true, // Compress old logs to save space
  maxSize: '20m',      // Rotate if file exceeds 20MB
  maxFiles: '14d'      // Keep logs for 14 days
});

const logger = winston.createLogger({
  transports: [transport]
});

Why Strategy Matters

Beyond just "clearing space," a rotation strategy is an architectural requirement for observability. Without it, you lack historical context for debugging. By setting a maxFiles limit, you balance the need for long-term troubleshooting data against the physical constraints of your server’s storage. Always ensure your rotation policy is calculated based on your average log generation rate so that you don't accidentally wipe out critical incident data prematurely.

Key Takeaway: Log rotation is a vital stability pattern that transforms an ever-growing, dangerous log file into a managed, finite lifecycle of archive files, preventing system failure while maintaining historical auditability.