Logo

dev-resources.site

for different kinds of informations.

Managing Logs with PM2 and pm2-logrotate

Published at
3/3/2024
Categories
node
pm2
logging
devops
Author
mochafreddo
Categories
4 categories in total
node
open
pm2
open
logging
open
devops
open
Author
11 person written this
mochafreddo
open
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.

pm2 Article's
30 articles in total
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Guia de Comandos PM2
Favicon
๐Ÿš€ Deploying Node.js Application with PM2, NGINX, and SSL Configuration ๐Ÿš€
Favicon
Monitoring PM2 in production
Favicon
Mastering PM2: Optimizing Node.js and Next.js Applications for Performance and Scalability
Favicon
Setting Up PM2 for Multi-User Access on Ubuntu Instance
Favicon
Manual deployment of NestJS and Angular applications on a dedicated server via "Docker Compose" and "PM2"
Favicon
Build applications on NestJS and Angular and run them in two versions: via PM2 and via Docker Compose
Favicon
An example of a simple update of NestJS-mod libraries
Favicon
Using pm2 to Manage Node.js Applications
Favicon
Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production
Favicon
Host Multiple Node Apps with nginx, pm2 with SSL certificate
Favicon
Node.js PM2 Orchestration Explained
Favicon
Managing Logs with PM2 and pm2-logrotate
Favicon
Streamlining PM2 Startup for Node.js Applications: A Comprehensive Guide
Favicon
Managing Next.js and NestJS Applications in Production with PM2
Favicon
Deploy a Full Stack Web App to VPS Server with NGINX andย PM2!
Favicon
Como utilizar o PM2 para gerenciar aplicaรงรตes
Favicon
Guide to Running a Node Server in Nx Monorepo using PM2 Process Manager
Favicon
How to run old nodejs Project into new nodejs project
Favicon
CentOS 7 NodeJS Kurulumu
Favicon
How to start node.js app with pm2
Favicon
How to start node.js app with pm2
Favicon
Deploying Multiple NodeJS Servers on a Single DigitalOcean Droplet; Managed by PM2, Without Using an ecosystem.config.js file
Favicon
Automatic deploys with PM2, Caddy, and GitHub Actions
Favicon
Utilizando PM2 (Basico)
Favicon
Experience on PM2 Tricks to manage your NodeJs processes
Favicon
Deploy Nest JS App using PM2 on Linux (Ubuntu) Server
Favicon
Install PM2 (Process Manager 2)
Favicon
Generate server block (virtual hosts) for nginx dynamically

Featured ones: