DEV Community

Geoffrey Kim
Geoffrey Kim

Posted on

Managing Logs with PM2 and pm2-logrotate

In this blog post, we'll dive into how to effectively manage your application logs when using PM2, a powerful process manager for Node.js applications. Proper log management is crucial for monitoring and troubleshooting applications. However, logs can quickly consume a significant amount of disk space. Here, we introduce pm2-logrotate, a module designed to automatically rotate logs, ensuring efficient log management without manual intervention.

Introduction to pm2-logrotate

pm2-logrotate is an essential module for anyone using PM2 to run their Node.js applications. It helps manage log file size and count, automatically rotating logs 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.

How to Install pm2-logrotate

Before diving into the configuration, ensure that you have PM2 installed. Then, you can easily add pm2-logrotate by running:

pm2 install pm2-logrotate
Enter fullscreen mode Exit fullscreen mode

Common Settings for pm2-logrotate

Let's explore some of the commonly used settings for pm2-logrotate that can help you manage your logs more effectively.

Max Size (max_size)

This setting determines the maximum size a log file can reach before being rotated. For example, setting this to '10M' rotates logs once they reach 10 megabytes.

pm2 set pm2-logrotate:max_size 10M
Enter fullscreen mode Exit fullscreen mode

Retention (retain)

Controls the number of rotated log files to keep, preventing your disk from filling up with old log files. Setting this to '30' keeps the most recent 30 logs.

pm2 set pm2-logrotate:retain 30
Enter fullscreen mode Exit fullscreen mode

Compress (compress)

Decides whether to compress rotated log files (gzip). This saves disk space at the expense of some CPU usage. Enabled by setting it to 'true'.

pm2 set pm2-logrotate:compress true
Enter fullscreen mode Exit fullscreen mode

Rotate Interval (rotateInterval)

Specifies the rotation frequency using a cron format. For daily rotation at midnight, use:

pm2 set pm2-logrotate:rotateInterval '0 0 * * *'
Enter fullscreen mode Exit fullscreen mode

Rotate Module (rotateModule)

Enables or disables rotation of PM2's own logs, which is especially useful for keeping PM2's logging in check.

pm2 set pm2-logrotate:rotateModule true
Enter fullscreen mode Exit fullscreen mode

DateFormat (dateFormat)

Used to format the filename of rotated logs. Setting this to 'YYYY-MM-DD' organizes logs by the date they were rotated.

pm2 set pm2-logrotate:dateFormat YYYY-MM-DD
Enter fullscreen mode Exit fullscreen mode

Worker Interval (workerInterval)

Determines how often pm2-logrotate checks log files for rotation, in seconds. A common setting is '30'.

pm2 set pm2-logrotate:workerInterval 30
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Regular Monitoring: Regularly monitor your log rotation settings to ensure they meet your application's needs without consuming excessive disk space.
  • Testing: Always test your pm2-logrotate settings in a staging environment before applying them to production.
  • Adjust According to Needs: The volume of logs can vary greatly between applications. Adjust settings like max_size and retain according to your specific needs.

Conclusion

Effective log management is pivotal for the smooth operation of any application. With pm2-logrotate, developers can ensure that logs are automatically managed, rotated, and maintained, preventing disk space issues and keeping logs organized and accessible. Adjusting settings like max size, compression, and retention allows for flexible and efficient log management tailored to your application's requirements.

Remember, the key to effective log management is finding the right balance between retaining necessary log information and conserving disk space.

Top comments (0)