π€ βWhy is my app crashing?β
π« βWhere did that error come from?β
π΅βπ« βIs my app running slow for some users?β
These are questions every developer faces β even the pros.
Thatβs where logging and monitoring come in π‘.
Itβs like giving your app a health checkup π©Ί, so you can catch bugs, measure performance, and sleep peacefully at night ποΈ.
Letβs break it down in a fun, friendly way β like Iβm teaching my little cousin!
π What Is Logging & Monitoring?
π Logging
Logging is like writing a diary π for your app.
It records what happens β good or bad.
Example:
βUser logged in at 3:14 PMβ
βError: Database connection failed ββ
π°οΈ Monitoring
Monitoring is like checking your appβs heart rate in real-time β€οΈβπ₯.
It tells you:
- How much memory your app is using
- If a process is stuck
- How many users are online
π― Why You Need It (Yes, You!)
Reason | Logging | Monitoring |
---|---|---|
Debug errors π | β | β |
Check performance π | β | β |
Track activity π | β | β |
Keep app healthy π©Ί | β | β |
Logging + Monitoring = A happy, healthy, maintainable app! π
π οΈ Tools Weβll Use
Tool | Use |
---|---|
Morgan | HTTP request logger π‘ |
Winston | Advanced logging engine π |
PM2 | Process and performance monitor π§ |
Weβll use them together in a Node.js + Express setup.
π§± Step-by-Step Logging with Morgan
1οΈβ£ Install Morgan
bashCopyEditnpm install morgan
2οΈβ£ Use It in Express App
jsCopyEditconst express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('tiny')); // or 'combined' for detailed logs
app.get('/', (req, res) => {
res.send('Hello from Web Codder! π');
});
app.listen(3000, () => console.log('Server running π'));
Now every request (GET, POST, etc.) is logged like:
sqlCopyEditGET / 200 12ms - 150B
Super helpful for debugging slow routes or API misuse!
π§± Structured Logging with Winston
1οΈβ£ Install Winston
bashCopyEditnpm install winston
2οΈβ£ Create a Logger File
jsCopyEdit// logger.js
const { createLogger, transports, format } = require('winston');
const logger = createLogger({
level: 'info',
format: format.combine(
format.timestamp(),
format.json() // logs as structured JSON
),
transports: [
new transports.Console(),
new transports.File({ filename: 'logs/error.log', level: 'error' }),
new transports.File({ filename: 'logs/combined.log' })
],
});
module.exports = logger;
3οΈβ£ Use Logger in App
jsCopyEditconst logger = require('./logger');
app.get('/test', (req, res) => {
logger.info('Test route accessed β
');
res.send('Logging works!');
});
app.get('/fail', (req, res) => {
logger.error('Something went wrong π’');
res.status(500).send('Error!');
});
Now your logs are organized by type and saved to files! π
π’ Logging Levels Explained
Level | Use Case |
---|---|
error | Critical problems π₯ |
warn | Potential issues β οΈ |
info | Regular activity π |
debug | Development logs π§ͺ |
Set different levels for dev and production π§.
π Real-Time Monitoring with PM2
1οΈβ£ Install PM2 Globally
bashCopyEditnpm install pm2 -g
2οΈβ£ Start Your App with PM2
bashCopyEditpm2 start app.js
Now PM2 keeps your app alive, even if it crashes!
3οΈβ£ View Logs & Status
bashCopyEditpm2 logs // view live logs
pm2 list // show all running apps
pm2 monit // real-time dashboard π
4οΈβ£ Auto-Restart on Server Reboot
bashCopyEditpm2 startup
pm2 save
π‘ Bonus: You can deploy multiple apps and scale them with PM2!
π Infographic (Insert Placeholder)

π§ Pro Tips
- β
Use
Morgan
for quick request tracking - β
Use
Winston
for deep debugging and file logs - β
Use
PM2
for real-time monitoring and zero-downtime restarts - π« Donβt log passwords or sensitive info!
- π Log only what you need in production to avoid bloated files
π Conclusion: Keep Your App Healthy π₯
Logging and monitoring are like having a doctor + bodyguard for your app.
They:
- Help you fix bugs faster π
- Make your app more stable βοΈ
- Give you peace of mind at night π
π£ Love Learning This Way?
Then youβll love whatβs coming next! π§βπ»
- π₯ Subscribe to Web Codder on YouTube
- π· Follow us on Instagram
- π¬ Join our WhatsApp dev community
Stay sharp, code smart, and track everything π§ π