Pino Redaction of Sensitive Log Fields
Prevent secrets from leaking into logs by configuring Pino redaction paths, nested wildcard censoring, and strict serializer boundaries.
Code
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:
pinois a real dependency, resolved by the package manager.
Interactive Tools for This Topic
PII Text CensorDetect and redact emails, phone numbers, credit card numbers, and US Social Security numbers in text, reporting how many of each were masked.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- Which packages must be installed first?
- Yes. The imports resolve to `pino`, which the package manager must provide.
Report a problem with this exampleOpens a pre-filled issue on GitHub.