PM2 Logrotate Master Guide

Complete Guide to PM2 Log Rotation: Installation, Configuration, and Management

Log management is crucial for production applications, and PM2's log files can quickly consume disk space if left unmanaged. This comprehensive guide covers everything you need to know about pm2-logrotate, from installation to optimization.

What is pm2-logrotate?

pm2-logrotate is an essential PM2 module that automatically rotates log files to prevent them from consuming all available disk space. It supports various features like setting maximum log size, compressing log files, and specifying the number of logs to retain.

Why Log Rotation Matters

The Problem

  • PM2 continuously writes to log files (stdout and stderr)

  • Log files can grow to gigabytes in size over time

  • Large log files cause performance issues:

    • Increased disk I/O operations

    • Memory pressure on the system

    • Slower file system operations

    • Backup complications

The Solution

Automatic log rotation prevents these issues by:

  • Limiting individual log file sizes

  • Maintaining a manageable number of historical logs

  • Compressing old logs to save space

  • Scheduling regular cleanup operations

Installation

Prerequisites

  • PM2 installed and running your applications

  • Node.js and npm available on your system

  • Appropriate permissions (root or sudo if PM2 runs as root)

Install pm2-logrotate Module

For root users:

Configuration Options Explained

Here's the recommended production configuration with detailed explanations:

Basic Configuration

Configuration Parameters Breakdown

max_size: 1G

  • Purpose: Maximum size a log file can reach before rotation

  • Options: Use K (kilobytes), M (megabytes), or G (gigabytes)

  • Impact: Smaller values = more frequent rotations but smaller files

  • Example: 10M, 500M, 2G

retain: 2

  • Purpose: Number of rotated log files to keep

  • Impact: Higher values preserve more history but consume more disk space

  • Recommendation: Balance between historical data needs and storage constraints

  • Example: With retain: 2, you'll have current log + 2 rotated files

compress: false

  • Purpose: Whether to compress rotated log files using gzip

  • Benefits: Saves significant disk space (typically 80-90% compression)

  • Drawbacks: Uses CPU during compression process

  • Use Case: Set to true for space-constrained servers, false for CPU-sensitive applications

dateFormat: 'YYYY-MM-DD_HH-mm-ss'

  • Purpose: Timestamp format for rotated log filenames

  • Result: Creates files like app-out.log-2025-10-10_15-30-45

  • Benefits: Easy identification and chronological sorting

  • Alternative formats: YYYY-MM-DD, YYYYMMDD, YYYY-MM-DD_HH-mm

workerInterval: 30

  • Purpose: How often (in seconds) to check log file sizes

  • Performance Impact: Lower values = more responsive but slightly higher CPU usage

  • Recommendation: 30-60 seconds for most applications

  • Range: 10 seconds (high-frequency apps) to 300 seconds (low-traffic apps)

rotateInterval: '0 0 * * *'

  • Purpose: Cron-format schedule for forced rotation

  • Format: second minute hour day month dayOfWeek

  • Example: '0 0 * * *' = daily at midnight

  • Common schedules:

    • '0 0 * * *' - Daily at midnight

    • '0 0 * * 0' - Weekly on Sunday

    • '0 2 * * *' - Daily at 2 AM

    • '30 1 * * *' - Daily at 1:30 AM

rotateModule: true

  • Purpose: Also rotate PM2's own internal module logs

  • Benefits: Prevents PM2 system logs from growing uncontrolled

  • Recommendation: Always set to true in production

Complete Setup Script

One-Liner Installation and Configuration

For Root Users

Managing Configuration

View Current Configuration

Edit Individual Settings

Apply Changes

After any configuration change, restart the module:

File Management Operations

View Log Files

Manual Log Operations

Clean Up Old Logs

Module Management

Check Module Status

Stop and Start Module

Remove Module

Performance Impact Analysis

Resource Usage

  • CPU Impact: Minimal during normal operation (~0.1% CPU)

  • Memory Usage: Typically 5-15 MB RAM

  • Disk I/O: Brief spikes during rotation events

  • Network: No network usage

Performance Considerations for Different Server Specs

4-core, 8GB+ RAM Server (Performance Settings)

High-Traffic Production (Optimized Settings)

Troubleshooting

Common Issues and Solutions

Module Not Rotating Logs

1

Step: Check if module is running

Run:

2

Step: Verify configuration

Run:

3

Step: Restart the module

Run:

High CPU Usage During Rotation

  • Cause: Large log files being compressed or rotated

  • Solution:

    • Reduce max_size setting

    • Disable compression (compress: false)

    • Increase workerInterval to reduce check frequency

Disk Space Still Growing

  1. Check retain setting:

  1. Verify old logs are being deleted:

  1. Manual cleanup if needed:

Module Installation Fails

  • Network Issues: Check internet connectivity and npm registry access

  • Permission Issues: Ensure proper user permissions for PM2 directory

  • Corrupted Installation: Try uninstalling and reinstalling:

Log Rotation Not Working as Expected

1

Step: Check cron format

Use a cron expression validator:

  • Use https://crontab.guru to verify your rotateInterval syntax

2

Step: Verify file permissions

Run:

3

Step: Check PM2 daemon status

Run:

Best Practices

Production Environment

  • Monitor disk space regularly

  • Set up alerts for disk usage > 80%

  • Test configuration changes in staging first

  • Keep retention period aligned with business requirements

  • Schedule log rotation during low-traffic periods

Development Environment

Staging Environment

Alternative Solutions

Native Logrotate (Linux Systems)

This creates a configuration file at /etc/logrotate.d/pm2-username with system-level log rotation.

Manual Cron Jobs

External Log Management

For enterprise environments, consider:

  • ELK Stack (Elasticsearch, Logstash, Kibana)

  • Fluentd for log collection and forwarding

  • Centralized logging services (AWS CloudWatch, Google Cloud Logging)

Monitoring and Maintenance

Regular Health Checks

Automated Monitoring Script

Conclusion

pm2-logrotate is essential for production PM2 deployments. The configuration provided in this guide balances performance, storage efficiency, and operational needs. Regular monitoring and proper configuration ensure your applications maintain optimal performance while preserving necessary log data for debugging and compliance requirements.

Remember to:

  • Test configuration changes in non-production environments first

  • Monitor disk usage regularly

  • Adjust settings based on your application's specific logging patterns

  • Keep retention policies aligned with your business and compliance requirements

By following this guide, you'll have a robust log management system that prevents disk space issues while maintaining the log data you need for effective application monitoring and troubleshooting.