🐞
Error Handling & Debugging

Debugging & Logging

morgan, DEBUG aur Node Inspector
💡 Logging EK FLIGHT ka BLACK BOX hai — jab kuch galat hota hai, tumhe yaad nahi rehta ki kya hua tha, lekin recording sab bata deti hai.

morgan EK HTTP request logger middleware hai — development mein "dev" format (colored, concise) aur production mein "combined" (Apache-style, detailed) use hota hai. Structured JSON logging ke liye pino ya winston better hain.

Express ke internals dekhne ke liye DEBUG=express:* env variable set karo — routing aur middleware matching ka poora trace milta hai. Breakpoints ke liye "node --inspect app.js" chalao aur Chrome DevTools ya VS Code attach kar do.

app.use(require("morgan")(process.env.NODE_ENV === "production" ? "combined" : "dev"));

# Express ka internal trace
DEBUG=express:* node app.js

# Breakpoint debugging
node --inspect app.js
🐞
Logging EK FLIGHT ka BLACK BOX hai — jab kuch galat hota hai, tumhe yaad nahi rehta ki kya hua tha, lekin recording sab bata deti hai.
1 / 2
⚡ Quick Recap
  • morgan = HTTP request logger, dev/combined formats ke saath
  • DEBUG=express:* se Express ka internal routing trace milta hai
  • node --inspect + DevTools se real breakpoints laga sakte ho
On this page (2 subtopics)

morgan ke predefined formats hain: "dev" (concise, colored status — development ke liye), "combined" (Apache combined log — production analysis ke liye), "tiny" (minimal). Custom tokens define karke request id ya user id bhi log kiya ja sakta hai.

const morgan = require("morgan");
morgan.token("id", (req) => req.id);
app.use(morgan(":id :method :url :status :response-time ms"));

DEBUG env variable Express ka internal namespace-based logging on karta hai — "express:router" routing decisions dikhata hai aur "express:application" app setup. Deep debugging ke liye node --inspect se Chrome DevTools ya VS Code attach karke real breakpoints lagaye ja sakte hain.

DEBUG=express:* node app.js
DEBUG=express:router node app.js

node --inspect-brk server.js     # pehli line par hi ruk jaayega