During an incident you do not read logs; you search them. "Show me every error for order o_123 in the last hour" is easy when logs are structured, and nearly impossible when they are free-form sentences.

From sentences to events

A typical unstructured log line:

2026-10-14 14:02:11 ERROR Payment failed for order o_123 user 42 after 3 retries (card_declined)

The same event, structured:

{
  "timestamp": "2026-10-14T14:02:11Z",
  "level": "error",
  "service": "checkout-api",
  "environment": "production",
  "message": "Payment failed",
  "orderId": "o_123",
  "userId": 42,
  "attempts": 3,
  "reason": "card_declined",
  "traceId": "5b8efff798038103d269b633813fc60c"
}

Now you can filter by reason, count failures per service, and jump to the trace.

Use levels consistently

LevelUse forExample
errorSomething failed and needs attentionPayment provider returned 500
warnUnexpected but handledRetrying after timeout
infoImportant business eventsOrder placed
debugDetails for developers, off in productionRequest payload shape

If everything is an error, nothing is.

Always include these fields

  • service and environment: which app, which deployment.
  • traceId (and spanId): links the log to the request's trace.
  • a stable message: "Payment failed", not "Payment failed for order o_123". Put variable parts in fields, so messages group together.
  • identifiers you will search by: order, user, tenant, job.

Correlate logs with traces

With OpenTelemetry, read the active trace id and add it to every log line:

import { trace } from '@opentelemetry/api';

function log(level, message, fields = {}) {
  const span = trace.getActiveSpan();
  const ctx = span?.spanContext();
  console.log(JSON.stringify({ level, message, ...fields, traceId: ctx?.traceId, spanId: ctx?.spanId, timestamp: new Date().toISOString() }));
}

From a slow trace you can then open exactly its logs, and from an error log you can open the full request.

What never to log

  • passwords, API keys, tokens, session cookies;
  • full card numbers or bank details;
  • personal data you do not need (full addresses, dates of birth);
  • entire request bodies by default.

Add a redaction step to your logger for known secret field names, and review new log statements in code review.

Keep volume under control

  • Log events, not every function call.
  • Sample very frequent info logs (for example health checks) or drop them.
  • Set retention by value: errors may be worth 30 to 90 days; debug logs a few days at most.

Ship logs reliably

Send logs in batches in the background, and never let logging failures break the app. A small queue with a periodic flush is enough for most services.

VitalRix logs accept JSON over HTTP in batches of up to 1,000 lines, offer search and live tail, and link every line that carries a trace id straight to its trace.

Example: a small Node.js logger

const queue = [];
const SECRET = /pass(word)?|token|secret|authorization|cookie/i;

function redact(fields) {
  return Object.fromEntries(Object.entries(fields).map(([k, v]) => [k, SECRET.test(k) ? '[redacted]' : v]));
}

export function log(level, message, fields = {}) {
  queue.push({ timestamp: new Date().toISOString(), level, message, service: 'checkout-api', ...redact(fields) });
}

setInterval(async () => {
  if (!queue.length) return;
  const logs = queue.splice(0, 1000);
  try {
    await fetch(process.env.LOGS_URL, { method: 'POST', headers: { 'content-type': 'application/json', 'x-api-key': process.env.LOGS_KEY }, body: JSON.stringify({ logs }) });
  } catch { /* never break the app because of logging */ }
}, 2000).unref();

In production, a mature library such as pino or winston does the same with less code; the principles are identical.

Searching logs during an incident

With structured fields, the useful questions become one filter each:

  • all errors from checkout-api in the last 30 minutes;
  • every log line for orderId = o_123;
  • count of reason = card_declined per hour this week;
  • everything for one traceId, across all services.

Live tail is the other half: watching new lines arrive while you deploy a fix tells you within seconds whether it worked.

What is structured logging?

Writing each log entry as a set of named fields, usually JSON, instead of a free-text sentence, so logs can be filtered, grouped and counted.

Why add trace IDs to logs?

A trace ID links every log line to the request that produced it, so you can move from a slow or failed trace to its logs and back.

What should never be logged?

Passwords, API keys, tokens, session cookies, full card numbers and personal data you do not need.

Try VitalRix free

Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.