0Pricing
Node.js Backend Development Bootcamp · บทเรียน

การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง

สร้างบันทึก JSON ที่เครื่องอ่านได้ และส่งต่อรหัสเชื่อมโยงผ่านบริบทอะซิงโครนัสเพื่อติดตามคำขอ

การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Structured Logging

In production, logs are read by machines before humans. A line like console.log('User ' + id + ' failed login') is a free-form string: to find it later you must write brittle regex and you cannot aggregate or filter reliably.

Structured logging emits each log as a JSON object with consistent fields. Log aggregators (Loki, Elasticsearch, Datadog, CloudWatch) index those fields so you can query level="error" AND userId=42 instantly.

  • Free-form: easy to write, painful to query.
  • Structured: one JSON object per line (NDJSON), trivially parseable.

A Minimal JSON Logger

The core idea is small: build a plain object, stamp it with a level and timestamp, and write one JSON string per line to stdout. The runtime or container collects stdout and ships it to your aggregator.

Each log is a single line of valid JSON. This format is called NDJSON (newline-delimited JSON) and every modern log backend understands it.

function log(level, message, fields = {}) {
  const entry = {
    level,
    time: new Date().toISOString(),
    message,
    ...fields,
  };
  process.stdout.write(JSON.stringify(entry) + '\n');
}

log('info', 'server started', { port: 3000 });
log('error', 'login failed', { userId: 42, reason: 'bad_password' });

Use a Real Logger: pino

Hand-rolling works for learning, but production apps use a battle-tested logger. pino is the de-facto choice for Node.js: it is extremely fast because it serializes JSON in a worker-friendly way and writes asynchronously.

  • logger.info(obj, msg) — the first arg is the object of fields, the second is the message string.
  • Levels: trace, debug, info, warn, error, fatal.
  • In production you pipe stdout to pino-pretty only in dev; raw JSON goes to prod.
const pino = require('pino');
const logger = pino({ level: 'info' });

logger.info({ port: 3000 }, 'server started');
logger.error({ userId: 42, reason: 'bad_password' }, 'login failed');

// Child loggers bind fields onto every subsequent log:
const reqLog = logger.child({ requestId: 'abc-123' });
reqLog.info('handling request');

The Problem: Tracing One Request

Under load, hundreds of requests interleave their logs. When user 42 reports an error, you need to see every log line that belongs to their request — across middleware, services, and database calls.

The solution is a correlation ID (also called request ID or trace ID): a unique identifier generated once per request and attached to every log emitted while handling that request.

  • Query correlationId="7f3a..." and the aggregator reconstructs the whole request timeline.
  • If the ID arrives in an incoming header, you can correlate logs across services.

Generating and Accepting the ID

At the edge of your system, read an incoming correlation header if a caller (gateway, upstream service) already set one; otherwise generate a fresh UUID. Always echo it back in the response so clients and proxies can record it too.

Standard header names are x-request-id or x-correlation-id. Reusing an inbound ID is what makes tracing work across service boundaries.

const { randomUUID } = require('crypto');

function correlationMiddleware(req, res, next) {
  const incoming = req.headers['x-correlation-id'];
  const correlationId = incoming || randomUUID();
  req.correlationId = correlationId;
  res.setHeader('x-correlation-id', correlationId);
  next();
}

module.exports = { correlationMiddleware };

The Naive Approach and Its Pain

The obvious move is to pass req.correlationId as an argument into every function and every log call. This works but does not scale: a deep call stack (controller → service → repository → helper) forces you to thread the ID through functions that otherwise have no reason to know about it.

This is prop drilling for the backend. You want the ID available implicitly to any code running during the request — without changing every function signature.

AsyncLocalStorage to the Rescue

Node.js ships AsyncLocalStorage (in the async_hooks module). It creates a store that stays bound to the current asynchronous execution context — surviving across await, callbacks, timers, and promises — without being shared between concurrent requests.

Think of it as request-scoped thread-local storage. You call als.run(store, callback) once per request; anywhere inside that callback (no matter how deep or how many awaits later) als.getStore() returns the same store.

const { AsyncLocalStorage } = require('async_hooks');
const als = new AsyncLocalStorage();

async function deep() {
  await new Promise((r) => setTimeout(r, 10));
  // Same store, even after awaits and timers:
  return als.getStore().correlationId;
}

async function main() {
  await als.run({ correlationId: 'req-1' }, async () => {
    console.log('inside:', await deep());
  });
  console.log('outside:', als.getStore());
}

main();

Wiring AsyncLocalStorage into the Request

Replace the naive middleware: instead of attaching the ID to req, run the rest of the request inside als.run() with the ID in the store. Now every line of code executed for this request — controllers, services, DB callbacks — can reach the ID via getStore().

Crucially, you must call next() inside the run callback so the downstream handlers inherit the context.

const { AsyncLocalStorage } = require('async_hooks');
const { randomUUID } = require('crypto');

const als = new AsyncLocalStorage();

function context() {
  return als.getStore() || {};
}

function correlationMiddleware(req, res, next) {
  const correlationId = req.headers['x-correlation-id'] || randomUUID();
  res.setHeader('x-correlation-id', correlationId);
  als.run({ correlationId }, () => next());
}

module.exports = { als, context, correlationMiddleware };

Auto-Injecting the ID into Every Log

The payoff: wrap your logger so it reads the correlation ID from AsyncLocalStorage automatically. Application code calls log.info('saved order') with no ID argument, yet every emitted line carries the right correlationId.

With pino you express this declaratively using the mixin option, which merges extra fields into every log record at write time.

const pino = require('pino');
const { als } = require('./context');

const logger = pino({
  level: 'info',
  mixin() {
    const store = als.getStore();
    return store ? { correlationId: store.correlationId } : {};
  },
});

// Anywhere deep in the request, no ID passed explicitly:
function saveOrder(order) {
  logger.info({ orderId: order.id }, 'order saved');
}

module.exports = { logger, saveOrder };

Propagating the ID to Downstream Calls

Correlation only spans services if you forward the ID on outbound requests. When your service calls another HTTP API, read the ID from context and set it as a header. The downstream service's middleware will reuse it, so both services' logs share one ID.

The same principle applies to message queues (put the ID in message metadata) and background jobs (store it on the job payload).

const { context } = require('./context');

async function callInventoryService(sku) {
  const { correlationId } = context();
  const res = await fetch('https://inventory.internal/check', {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
      'x-correlation-id': correlationId,
    },
    body: JSON.stringify({ sku }),
  });
  return res.json();
}

module.exports = { callInventoryService };

Production Hygiene

A few rules keep your structured logs clean and safe:

  • Never log secrets or PII — passwords, tokens, full card numbers. Configure pino redact paths (e.g. ['req.headers.authorization', '*.password']).
  • Log at the right level — info for business events, warn for recoverable issues, error with the serialized error object for failures.
  • Keep field names stable — correlationId always means the same thing; dashboards and alerts depend on it.
  • One JSON object per line — do not pretty-print in production; it breaks NDJSON parsing.
const pino = require('pino');

const logger = pino({
  level: process.env.LOG_LEVEL || 'info',
  redact: ['req.headers.authorization', 'password', '*.password', 'creditCard'],
});

logger.info({ user: { id: 7, password: 'hunter2' } }, 'login');
// -> password is replaced with [Redacted] in the output

Quick Check

You need the correlation ID available to every function in a deep call stack during a request, without passing it as an argument and without leaking it between concurrent requests. What is the correct mechanism in Node.js?

Recap

You learned how to make logs both machine-parseable and traceable:

  • Structured logging emits one JSON object per line (NDJSON) so aggregators can index and query fields.
  • pino is the standard fast Node.js logger; the field object comes first, the message second.
  • A correlation ID is generated or reused from an inbound header once per request and echoed in the response.
  • AsyncLocalStorage threads that ID through the whole async call stack without changing function signatures, and isolates concurrent requests.
  • Pino's mixin auto-injects the ID into every log; forwarding it as an outbound header extends tracing across services.
  • Practice good hygiene: redact secrets, use correct levels, keep field names stable, never pretty-print in production.

คำถามที่พบบ่อย

บทเรียน “การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง”

สร้างบันทึก JSON ที่เครื่องอ่านได้ และส่งต่อรหัสเชื่อมโยงผ่านบริบทอะซิงโครนัสเพื่อติดตามคำขอ คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การบันทึกแบบมีโครงสร้างด้วยรหัสเชื่อมโยง
  2. การติดตามแบบกระจายด้วยสแปน OpenTelemetry
  3. การเปิดเผยเมทริกซ์แอปพลิเคชันและเมธอด RED
  4. การส่งต่อบริบทด้วย AsyncLocalStorage
← กลับไปที่ Node.js Backend Development Bootcamp