How to Monitor Your Node.js App with PM2 and Log Rotation

Aamir Khan
0
How to Monitor Your Node.js App with PM2 and Log Rotation

How to Monitor Your Node.js App with PM2 and Log Rotation

As your Node.js application grows, keeping it stable and running smoothly becomes a priority. This is where PM2 comes in—a powerful process manager for Node.js that not only monitors your app but also ensures it automatically restarts if something goes wrong. In this guide, I’ll walk you through how to set up PM2, monitor your app effectively, and enable log rotation to keep your logs organized.

If you’re a junior developer or self-taught coder, understanding tools like PM2 can make a big difference in deploying reliable applications. It’s like having a personal assistant watching your server 24/7!

What is PM2?

PM2 is a production-grade process manager for Node.js applications. It allows you to:

  • Run apps in the background (daemonize them).
  • Automatically restart apps on failure.
  • Monitor resource usage like CPU and memory.
  • Handle multiple app instances using clustering.

Why Use PM2 for Monitoring?

Node.js apps, when deployed directly, can crash due to unhandled errors or server restarts. PM2 provides:

  • Automatic restarts on crashes.
  • Real-time monitoring of processes.
  • Organized logging for debugging.
  • Built-in support for load balancing with clustering.

Installing PM2

To get started, install PM2 globally using npm:

npm install -g pm2

Start Your Node.js App with PM2

pm2 start app.js

You can also assign a custom name to your process:

pm2 start app.js --name "my-node-app"

Monitoring Your App

PM2 provides real-time monitoring with:

pm2 monit

You can check logs using:

pm2 logs

Setting Up Log Rotation

Without log rotation, logs can grow indefinitely and take up storage space. PM2 has a built-in module for this:

pm2 install pm2-logrotate

To configure log rotation settings:

pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 5
pm2 set pm2-logrotate:compress true
pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss

This setup rotates logs when they exceed 10 MB, keeps the last 5 logs, and compresses older logs.

Real-World Example

Suppose you’re running an Express.js server:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello PM2 Monitoring!');
});

app.listen(3000, () => console.log('Server running on port 3000'));

Start and monitor it with:

pm2 start server.js --name "express-server"

Related Resources

Conclusion

PM2 simplifies managing and monitoring your Node.js applications. With log rotation, you can keep your logs tidy and avoid disk space issues. Whether you’re building a small side project or a large-scale API, this setup is essential for smooth operations.

Have you used PM2 before? Share your experience in the comments, or try setting it up on your next project!

💖

Enjoyed the Post?

If my blog helped you, please consider supporting me!

Support Me 💖

Post a Comment

0 Comments

Hi Everyone, please do not spam in comments.

Post a Comment (0)

Made with Love by

Welcome to Code Journey with Aamir – your one-stop hub for full-stack development tutorials, projec…