Pino Redaction of Sensitive Log Fields

Prevent secrets from leaking into logs by configuring Pino redaction paths, nested wildcard censoring, and strict serializer boundaries.

JavascriptResilience

Code

javascriptsnippet
22 lines
import { pino } from 'pino';

const logger = pino({
  redact: {
    paths: [
      'req.headers.authorization',
      'req.headers["x-api-key"]',
      'password',
      '*.token',
      'user.credentials.*',
    ],
    censor: (value) => (typeof value === 'string' ? '[redacted:' + value.length + ']' : '[redacted]'),
  },
});

const req = {
  id: 'req-91',
  headers: { authorization: 'Bearer eyJhbGciOi', 'x-api-key': 'sk-live-9f2' },
};

logger.info({ req, user: { token: 'abc123', name: 'ada' } }, 'authenticated request');
logger.info({ password: 'hunter2', nested: { token: 'zzz' } }, 'form submitted');

How the snippet works

Everything borrowed from pino is visible in one line: pino.

Top-level bindings logger, req hold the demo state — nothing sits inside a function scope.

Emits through the .info level methods with structured payloads, so downstream transports can route by level and field.

What to watch for

  • Plan for an install step: pino is a real dependency, resolved by the package manager.

Interactive Tools for This Topic

FAQ

Which packages must be installed first?
Yes. The imports resolve to `pino`, which the package manager must provide.