← Back to Node.js Course | Chapter 10: Deployment & Best Practices | Lesson 5 of 7

Logging कैसे करें

अच्छी logs आपको यह समझने में मदद करती हैं कि आपकी app ने क्या किया, खासकर कुछ गलत होने के बाद।

In this page:

  1. Logging
Syntax
javascript
console.error(message);
console.warn(message);
console.info(message);
console.debug(message);

Logging

Log levels इस्तेमाल करें (error, warn, info, debug), timestamps और context include करें, और ऐसे structured JSON logs prefer करें जिन्हें tools search कर सकें। pino और winston जैसी libraries levels और formats handle करती हैं। कभी passwords या tokens log न करें।

Note: logs को stdout में लिखें और platform को इन्हें collect करने दें।

उदाहरण: Logging

javascript
function log(level, msg, extra = {}) {
  console.log(JSON.stringify({ level, msg, ...extra, ts: "2024-01-01T00:00:00.000Z" }));
}
log("info", "server started", { port: 3000 });
log("error", "db connection failed", { attempt: 2 });

// Output:
// {"level":"info","msg":"server started","port":3000,"ts":"2024-01-01T00:00:00.000Z"}
// {"level":"error","msg":"db connection failed","attempt":2,"ts":"2024-01-01T00:00:00.000Z"}

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. secrets log करना
  2. सब कुछ के लिए console.log इस्तेमाल करना
  3. ऐसे unstructured messages जिन्हें search नहीं किया जा सकता
चैप्टर सारांश
  • Log levels इस्तेमाल करें
  • Structured JSON logs इस्तेमाल करें
  • Context और timestamps include करें
  • कभी secrets log न करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.