๐Ÿ“‹ 3.12. Logging & Monitoring โ€“ Tracking Application Health

Table of Contents

๐Ÿค” โ€œ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!)

ReasonLoggingMonitoring
Debug errors ๐Ÿ›โœ…โŒ
Check performance ๐Ÿ“ˆโŒโœ…
Track activity ๐Ÿ“šโœ…โœ…
Keep app healthy ๐Ÿฉบโœ…โœ…

Logging + Monitoring = A happy, healthy, maintainable app! ๐ŸŽ‰


๐Ÿ› ๏ธ Tools Weโ€™ll Use

ToolUse
MorganHTTP request logger ๐Ÿ“ก
WinstonAdvanced logging engine ๐Ÿ““
PM2Process 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

LevelUse Case
errorCritical problems ๐Ÿ”ฅ
warnPotential issues โš ๏ธ
infoRegular activity ๐Ÿ“
debugDevelopment 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)

Orig
๐Ÿ“‹ 3.12. Logging & Monitoring - Tracking Application Health 2


๐Ÿง  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! ๐Ÿง‘โ€๐Ÿ’ป

Stay sharp, code smart, and track everything ๐Ÿง ๐Ÿš€

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts