Course contentsShow
← Node.js
Lesson 1497 of 1,98013. Error Handling and Resilient SystemsFree lesson

Log Rotation Policy Management

You will configure log rotation to prevent infinite log growth and eventual server filesystem failure.

In this lesson, you will learn how to implement automated log rotation policies to prevent uncontrolled file growth from crashing your Node.js production environments.

The Problem: Silent Disk Exhaustion

In Node.js, logs are often written to local files using streams. If you simply append to app.log indefinitely, the file size will eventually consume all available disk space. When the filesystem reaches 100% capacity, your application will fail to write new data, likely triggering a cascade of failures, such as the inability to create session files or process temporary buffers, leading to "Disk Full" scenarios.

Implementing Rotation Policies

To prevent this, we use Log Rotation, a strategy where the current log file is periodically closed, renamed, and archived based on specific policies—typically by file size (e.g., max 10MB) or by time (e.g., daily).

Instead of writing raw logs directly to fs.createWriteStream, we use a library like rotating-file-stream. This handles the heavy lifting of managing file descriptors and enforcing a "retention policy," which dictates how many old log files to keep before deleting them.

const { createStream } = require('rotating-file-stream');

// Configure rotation: 10MB max size, compress files, keep 5 logs
const logStream = createStream('app.log', {
  size: '10M',
  interval: '1d',
  compress: 'gzip',
  maxFiles: 5,
  path: './logs'
});

// Use it as a standard Writable stream
process.stdout.pipe(logStream);
process.stderr.pipe(logStream);

Strategic Considerations

When configuring your policy, ensure your retention count matches your disk capacity. If your application generates 100MB of logs daily and you keep 5 compressed logs, you are effectively reserving at least 500MB of disk space for logs.

Furthermore, ensure that your log rotation policy is decoupled from your application logic. By using a stream-based approach, the application remains oblivious to the rotation events, maintaining resilience even when the underlying file is swapped. Always verify that your process has the appropriate permissions to rename and delete files in your log directory, otherwise, rotation will fail and your disk will fill up despite the policy.

Key Takeaway: Log rotation is a critical safeguard that enforces a fixed upper bound on disk usage, ensuring that infinite log growth does not lead to a system-wide crash.