๐ค โ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 ๐ง ๐