Skip to content

@edgetime/log

A structured JSON logger with levels, child loggers, redaction and bounded records. No dependencies.

npm install @edgetime/log

A structured logger that writes one JSON object per record. No dependencies.

import { createLogger } from '@edgetime/log';
const log = createLogger({ base: { service: 'orders' } });
log.info('order placed', { orderId: 'o_123' });
// {"service":"orders","orderId":"o_123","level":"info","message":"order placed","time":"..."}
const requestLog = log.child({ requestId });
requestLog.error('charge failed', { error });
Option Default Purpose
level info Records below this level are dropped. Levels are debug, info, warn and error, in any letter case; any other value throws.
base none Fields added to every record.
redact DEFAULT_REDACT Words that mark a field as sensitive. A field whose name contains one has its value replaced with [redacted], at any depth.
scrub true Also replaces secrets found inside string values and the message with [redacted]; see below.
sink console Receives each record. The default writes a JSON line with the console method for the level. It may be async; a rejection is ignored.

DEFAULT_REDACT is authorization, cookie, password, passwd, secret, token, apikey, privatekey, credential, jwt, session, signature, passphrase, pwd, bearer and accesskey, plus the whole names auth, xauth, key, pass, otp, pin, sig and dsn. Matching ignores case and punctuation, so token also covers access_token, refreshToken and tokens, and apikey covers x-api-key. A word written with a leading =, such as =key, matches only a field of exactly that name, so key is redacted and keyboard is not. Passing redact replaces the list, so spread the default to extend it.

A header entry is redacted by its name in both common shapes: a [name, value] pair and a { name, value } object. A URL is logged without its user, password, query and fragment.

Values are scrubbed as well, including the message and the message and stack of a logged error: Bearer and Basic credentials, compact JWT and JWE tokens, and the user, password, query and fragment of an http(s) URL are replaced with [redacted]. A word after Bearer or Basic that reads as prose, such as Bearer token missing, is kept. scrubSecrets(text) applies the same rules on its own. Turn it off with scrub: false.

Scrubbing recognises those shapes only. A secret in any other form, under a field name not in the list, is not detected, so keep secrets out of log calls rather than relying on it.

A logging call never throws, and a record is bounded. Nesting stops at 6 levels, a record holds at most 2000 objects and about 64 000 characters of names and values, an object is cut at 200 fields, an array at 100 items and a string or message at 8192 characters; what is left out is marked [truncated]. A reference back to an enclosing object becomes [circular], and a value that cannot be read becomes [unreadable] without affecting the rest of the record. Map, Set, URL and Headers are logged by content, and binary data by its size.

child(fields) returns a logger that adds fields to every record. Use it for request-scoped values; the logger itself holds no request state. The fields of base and child() are copied, redacted and bounded once, when the logger is made, so changing an object afterwards does not change what later records show, and each record’s own fields have the full size bound to themselves.

Errors in fields are flattened to plain data, following cause chains. serializeError does the same on its own.


Source: packages/log on GitHub.