Blog Hoch
Mar 2026·2 tup leghDe'wI' chenmoHta'

Structured Logging: printf debugging scale Qu'be'

#backend#observability#javascript

console.log('user logged in', userId) Dun - 'ach servers wa'maH, log lines wa' SaD Hoch lup 'ej customer wa' requests wej services Daq 20 tup Dung tu' poQ. plain-text logs debugging tool pagh, haystack choH. Structured logging Qotlh, adopt Huch pagh.

string logs Qagh

traditional log line human-readable sentence:

2026-03-17T09:14:02Z User 4821 logged in from 203.0.113.4 after 2 failed attempts

isolation Daq laD Dun. large scale Daq query Qatlh. specific IP login Hoch tu' vaj regex free text Daq, wording choHbe' Qoy (saved queries Qaw'), fields (user ID, IP, attempt count) parseable position Daq Qoy.

structured logging: data log, sentence pagh

structured log line machine-parseable record (JSON), context Hoch field:

logger.info('user_login', {
  userId: 4821,
  ip: '203.0.113.4',
  failedAttempts: 2,
});
{"level":"info","event":"user_login","userId":4821,"ip":"203.0.113.4","failedAttempts":2,"timestamp":"2026-03-17T09:14:02.118Z"}

"IP login Hoch" ip field query, regex pagh. "user 4821 events wa' rep Dung" filter, grep pagh. event name (user_login) stable - human-readable version write time Daq chenmoH pagh; formatting display concern.

minimal structured logger

heavyweight framework poQ pagh. logger base fields consistent chel 'ej JSON reH emit:

function createLogger(baseFields = {}) {
  const emit = (level, event, fields = {}) => {
    const record = {
      level,
      event,
      timestamp: new Date().toISOString(),
      ...baseFields,
      ...fields,
    };
    process.stdout.write(JSON.stringify(record) + '\n');
  };

  return {
    info: (event, fields) => emit('info', event, fields),
    warn: (event, fields) => emit('warn', event, fields),
    error: (event, fields) => emit('error', event, fields),
    child: (extraFields) => createLogger({ ...baseFields, ...extraFields }),
  };
}

const logger = createLogger({ service: 'auth-api' });
logger.info('user_login', { userId: 4821, ip: '203.0.113.4' });

child() qatlh law': context wa'logh attach, call Hoch Daq flow.

request-scoped context: real payoff

request wa' log line Hoch request ID rap automatic (call site explicit pass pagh).

import { AsyncLocalStorage } from 'node:async_hooks';

const requestContext = new AsyncLocalStorage();

function withRequestContext(requestId, fn) {
  return requestContext.run({ requestId }, fn);
}

function requestLogger(event, fields = {}) {
  const context = requestContext.getStore() ?? {};
  logger.info(event, { ...context, ...fields });
}

// In middleware, per incoming request:
app.use((req, res, next) => {
  withRequestContext(crypto.randomUUID(), next);
});

// Anywhere deeper in the call stack, no requestId parameter needed:
requestLogger('db_query_start', { table: 'orders' });
requestLogger('db_query_end', { table: 'orders', durationMs: 12 });

log line Hoch request poH Dung (wej services tlhoch, callback, error handler) requestId rap. "request failing Hoch tu'" archaeology pagh, wa' filter.

log field Daq nuq

  • IDs, descriptions pagh. userId: 4821, "for user number 4821" pagh. reader user table join.
  • Durations numbers, wa' unit. durationMs: 340, milliseconds reH. "340ms" 2.1 (seconds) mix Qo'.
  • Stable event names. user_login, sentence pagh. event name API rap - rename dashboard alert Qaw'.
  • Errors structured objects, .message neH pagh:
logger.error('payment_failed', {
  orderId: order.id,
  error: {
    message: err.message,
    name: err.name,
    stack: err.stack,
  },
});

nuq replace pagh

structured logging metrics traces replace pagh - observability wej leg, "nuq qaS, order, wa' request" vaD Dun. Metrics "law' mI'" Huch pagh; traces "poH nuqDaq" services law'; logs "values real nuq". question Daq jang. customer wa' failed request vaD logs reH.

yaj

string logs structured logs choH write time Daq Dach (object nob, sentence pagh) 'ej read time Daq tIn - field Hoch queryable. request-scoped context chel vaj printf debugging laHbe' Qu': wa' request log lines Hoch tu', others firehose vo', wa' query.

chu'Event Sourcing vs Traditional CRUDqanCORS wa'logh Hoch vaD yaj