DEV Community

Manoj Swami
Manoj Swami

Posted on

Managing PM2 Logs: Preventing Performance Issues in Node.js Applications

If you're running Node.js applications in production, you've probably encountered PM2, the popular process manager that helps keep your applications running smoothly. While PM2 offers excellent features for managing and monitoring your applications, there's a potential issue that many developers overlook: log file growth.

In this article, we'll explore how large PM2 log files can impact your application's performance and provide practical solutions to manage these logs effectively.

The Problem: How Large PM2 Logs Impact Performance

When PM2 manages your Node.js applications, it creates log files to capture standard output and error streams. Over time, these logs can grow extremely large—sometimes reaching gigabytes in size. A 20GB log file isn't uncommon in busy applications that have been running for months without proper log management.

These oversized log files can cause several performance issues:

  1. Increased Disk I/O: PM2 continuously writes to log files. When these files become enormous, write operations become more resource-intensive, competing with your application for disk access.

  2. Memory Pressure: Managing very large files requires additional memory allocation, potentially reducing what's available for your Node.js application.

  3. Slower File System Operations: Operations that need to seek through the file become slower as file size increases.

  4. Backup Complications: Large log files make backups more time-consuming and storage-intensive.

Let's look at several approaches to solve this problem, from simplest to most sophisticated.

Solution 1: Manual Log Truncation

The quickest way to address an immediate issue with oversized log files is to manually truncate them. Here's how:

# Truncate all PM2 logs
pm2 flush

# Truncate a specific application's logs
truncate -s 0 ~/.pm2/logs/your-app-name-out.log
truncate -s 0 ~/.pm2/logs/your-app-name-error.log
Enter fullscreen mode Exit fullscreen mode

The truncate command reduces the file size to zero without deleting the file itself, allowing PM2 to continue writing to the same file.

Solution 2: Scheduled Log Cleanup with Cron

To prevent log files from growing too large in the first place, you can set up a cron job to regularly manage your logs. Here are several approaches:

Option A: Truncate Log Files

# Add to crontab (crontab -e)
# Run at midnight every day
0 0 * * * truncate -s 0 ~/.pm2/logs/*.log
Enter fullscreen mode Exit fullscreen mode

Option B: Delete Log Files

# Add to crontab (crontab -e)
# Run at midnight every day
0 0 * * * rm -f ~/.pm2/logs/*.log
Enter fullscreen mode Exit fullscreen mode

Option C: Clear Log Directory Contents

# Add to crontab (crontab -e)
# Run at midnight every day
0 0 * * * rm -rf ~/.pm2/logs/* && mkdir -p ~/.pm2/logs
Enter fullscreen mode Exit fullscreen mode

Option D: Delete Logs Older Than X Days

# Add to crontab (crontab -e)
# Delete logs older than 7 days
0 0 * * * find ~/.pm2/logs -type f -name "*.log" -mtime +7 -delete
Enter fullscreen mode Exit fullscreen mode

Solution 3: Using PM2's Built-in Flush Command with Cron

PM2 provides a flush command that clears all application logs:

# Add to crontab (crontab -e)
0 0 * * * pm2 flush
Enter fullscreen mode Exit fullscreen mode

This approach is simpler than manually truncating specific files and works across all your PM2-managed applications.

Solution 4: PM2 Logrotate Module (Recommended)

The most elegant solution is to use PM2's official log rotation module. This approach gives you the most control and follows best practices for log management.

Step 1: Install the module

pm2 install pm2-logrotate
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure rotation settings

# Set maximum size before rotation
pm2 set pm2-logrotate:max_size 10M

# Keep only 10 rotated files
pm2 set pm2-logrotate:retain 10

# Compress rotated logs to save space
pm2 set pm2-logrotate:compress true

# Rotate logs daily at midnight
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'

# Use date-based suffixes for rotated logs
pm2 set pm2-logrotate:dateFormat YYYY-MM-DD
Enter fullscreen mode Exit fullscreen mode

With these settings, your logs will automatically rotate when they reach 10MB in size or at midnight each day, whichever comes first. Only the 10 most recent log files will be kept, and they'll be compressed to save space.

Solution 5: External Logging Services

For more advanced needs, consider moving beyond local file logging altogether:

  1. Use a dedicated logging service like Loggly, Papertrail, or ELK Stack
  2. Implement a custom logging solution using Winston or Bunyan that sends logs to external storage
  3. Use container orchestration logging like Kubernetes logging with Fluentd

Setting Up Time-Based Log Management for Different Time Zones

If your server runs in a different time zone than your local time, you'll need to adjust your cron schedule accordingly. For example, if you want logs to rotate at 6 AM Indian Standard Time (IST), which is UTC+5:30, you would use:

# 6 AM IST = 00:30 UTC
# Add to crontab (crontab -e)
30 0 * * * pm2 flush
Enter fullscreen mode Exit fullscreen mode

Here's how to calculate this for other time zones:

  1. Identify your target local time (e.g., 6 AM IST)
  2. Determine the UTC offset for that time zone (IST is UTC+5:30)
  3. Subtract the offset from your target time (6:00 - 5:30 = 00:30 UTC)
  4. Use the resulting UTC time in your cron schedule

Best Practices

Regardless of which solution you choose, follow these best practices:

  1. Monitor log growth: Regularly check log file sizes with du -sh ~/.pm2/logs/
  2. Backup important logs: Before implementing any cleanup, archive logs that contain valuable information
  3. Test your solution: Verify that your applications continue to work properly after log cleanup
  4. Document your approach: Make sure your team knows how logs are managed
  5. Consider compliance requirements: Some industries have specific log retention requirements

Conclusion

Large PM2 log files can significantly impact your Node.js application's performance. By implementing one of the approaches outlined in this article, you can prevent these issues and ensure your applications run smoothly.

For most production environments, the PM2 logrotate module (Solution 4) offers the best balance of simplicity and control. However, any of these methods will help prevent the performance problems associated with oversized log files.

Remember that effective log management is an essential part of maintaining healthy production applications. Taking the time to set up proper log rotation will save you from troubleshooting mysterious performance issues down the road.

Happy Coding!

Top comments (0)