Monitoring and Logging on EC2 Keeping Your Node.js App Healthy in Production
Learn how to monitor your Node.js application on AWS EC2 using PM2 monitoring, log rotation, CloudWatch, and external tools to keep your app healthy and catch issues early.
Monitoring and Logging on EC2
Monitoring your Node.js app in production is essential for catching issues before they affect users. This guide covers PM2 monitoring, log management, and external monitoring tools.
PM2 Monitoring
PM2 provides built-in monitoring tools:
# Real-time monitoring (CPU, memory, logs) pm2 monit # View all processes and their status pm2 status # View logs (streaming) pm2 logs devtinder-api # View only error logs pm2 logs devtinder-api --err # View last 100 lines pm2 logs devtinder-api --lines 100 # View process details pm2 describe devtinder-api
PM2 Log Rotation
Without log rotation, PM2 logs can fill up your disk. Install the log rotation module:
pm2 install pm2-logrotate
Default settings:
- Max size: 10 MB per file
- Retain: 30 files
- Compress old logs: yes
Customize:
pm2 set pm2-logrotate:max_size 5M pm2 set pm2-logrotate:retain 7 pm2 set pm2-logrotate:compress true
Nginx Logs
# Access log (every HTTP request) sudo tail -f /var/log/nginx/access.log # Error log sudo tail -f /var/log/nginx/error.log # Check for 4xx and 5xx errors sudo grep -E " 4[0-9]{2} | 5[0-9]{2} " /var/log/nginx/access.log
Nginx log rotation is configured by default in /etc/logrotate.d/nginx.
System Logs
# System messages sudo tail -f /var/log/syslog # Authentication logs (SSH logins) sudo tail -f /var/log/auth.log # Check for failed SSH attempts sudo grep "Failed password" /var/log/auth.log | tail -20
AWS CloudWatch Monitoring
Install the CloudWatch agent to send metrics and logs to AWS:
# Install CloudWatch agent sudo apt install -y amazon-cloudwatch-agent # Configure it sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
CloudWatch can monitor:
- CPU utilization
- Memory usage (requires agent)
- Disk space
- Network traffic
- Custom application metrics
Set up alarms:
- CPU > 80% for 5 minutes
- Memory > 80% for 5 minutes
- Disk space < 10%
- 5xx error rate > 5%
External Uptime Monitoring
Use external services to monitor your app from outside:
UptimeRobot (free):
- Go to https://uptimerobot.com
- Add a monitor for
https://yourdomain.com/api/health - Set check interval (5 minutes)
- Get email/SMS alerts when it goes down
Pingdom (paid):
- More detailed monitoring
- Performance insights
- Status pages
Health Check Endpoint
Create a health check endpoint in your Node.js app:
router.get("/health", (req, res) => { res.json({ status: "ok", uptime: process.uptime(), timestamp: new Date().toISOString(), environment: process.env.NODE_ENV }); });
This endpoint should:
- Return 200 when the app is healthy
- Not require authentication
- Be fast (no database queries)
- Include basic info (uptime, environment)
Error Tracking with Sentry
npm install @sentry/node
const Sentry = require("@sentry/node"); Sentry.init({ dsn: process.env.SENTRY_DSN, environment: process.env.NODE_ENV }); // Express error handler app.use(Sentry.Handlers.errorHandler()); app.use((err, req, res, next) => { res.status(err.statusCode || 500).json({ message: err.message }); });
Sentry captures unhandled errors, sends alerts, and provides a dashboard for tracking issues.
Performance Monitoring with PM2 Plus
pm2 plus
PM2 Plus (paid) provides:
- Real-time metrics dashboard
- Memory leak detection
- Event tracking
- Distributed tracing
- Exception reporting
Log Aggregation
For production, send logs to a centralized service:
CloudWatch Logs:
# Configure CloudWatch agent to send PM2 logs sudo nano /opt/aws/amazon-cloudwatch-agent/bin/config.json
ELK Stack (Elasticsearch, Logstash, Kibana):
- Self-hosted log aggregation
- Powerful search and visualization
- Requires separate server
Datadog:
- Cloud-based monitoring
- APM (Application Performance Monitoring)
- Log management
- Paid service
The Takeaway
Monitoring your EC2 Node.js app involves: PM2 built-in monitoring (pm2 monit, pm2 logs), log rotation (pm2-logrotate), Nginx access and error logs, system logs, AWS CloudWatch for metrics and alarms, external uptime monitoring (UptimeRobot), a /health endpoint, error tracking with Sentry, and log aggregation. Set up alerts so you know about issues before your users do.
Use pm2 monit for real-time CPU/memory monitoring, pm2 logs for streaming logs, pm2 status for process status. For external monitoring, use UptimeRobot for uptime checks and Sentry for error tracking. Set up a /health endpoint for health checks.
Install pm2-logrotate with pm2 install pm2-logrotate. Configure max file size (pm2 set pm2-logrotate:max_size 5M), retention (pm2 set pm2-logrotate:retain 7), and compression (pm2 set pm2-logrotate:compress true).
A 200 status with JSON containing status: 'ok', uptime, timestamp, and environment. It should not require authentication, should be fast (no database queries), and should be accessible at /api/health for external monitoring tools.
Install @sentry/node, initialize with your DSN in app.js, and add Sentry.Handlers.errorHandler() before your global error handler. Sentry captures unhandled errors, sends alerts, and provides a dashboard for tracking issues.
CPU > 80% for 5 minutes, memory > 80% for 5 minutes (requires CloudWatch agent), disk space < 10%, and 5xx error rate > 5%. Set up SNS notifications to receive email or SMS alerts when alarms trigger.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

