PrivateconstructorConstructs a Logger.
Minimum weight to emit.
Fields repeated on every line.
Which context fields reach the line.
Private ReadonlycontextFields repeated on every line.
Private ReadonlyfieldsWhich of those fields the deployment actually wants ingested.
Private ReadonlythresholdMinimum weight a line must carry to be emitted.
Derives a logger with extra context, sharing this one's threshold.
Fields to add to every line.
The derived logger.
PrivatecomposeAssembles the object that becomes the line.
Severity of the line.
What happened, as a dotted event name.
Optionaldata: Record<string, unknown>
Structured detail.
Optionalmessage: string
A sentence for someone reading the stream.
The line, ready to serialise.
The three reserved keys are written first so a raw line reads
level-then-event-then-message, and re-asserted on every merge so nothing
can displace them. time sits behind the allow-list like any other field:
a runtime that stamps its own ingestion timestamp does not need a second
one, and it is computed here rather than carried in the context so it
records when the line was written and not when the logger was built.
Emits a debug line.
What happened, as a dotted event name.
Optionaldata: Record<string, unknown>
Structured detail.
Optionalmessage: string
A sentence for someone reading the stream. Defaults to the event name.
PrivateemitWrites one line through the console method matching its level.
Severity of the line.
What happened, as a dotted event name.
Optionaldata: Record<string, unknown>
Structured detail.
Optionalmessage: string
A sentence for someone reading the stream.
Context and payload are both flattened onto the line rather than nested,
so a collector indexes status rather than data.status and a dashboard
column reads as a field name instead of a path.
They are not treated alike. Context is ambient — stamped by middleware for the span of a request — and passes through the allow-list, because a deployment should be able to decide it does not want the point of presence on every line. Payload was typed out at the call site for this one call, so it is emitted whole: filtering it would silently discard the answer someone went to the trouble of logging.
Emits an error line.
What happened, as a dotted event name.
Optionaldata: Record<string, unknown>
Structured detail.
Optionalmessage: string
A sentence for someone reading the stream. Defaults to the event name.
StaticfromBuilds a logger for the current environment.
Anything carrying an optional LOG_LEVEL, SERVICE_NAME and
LOG_FIELDS. Tolerates null.
Fields to repeat on every line.
A logger honouring the configured (or LOG_LEVEL) threshold, and
emitting the context fields the deployment declared.
The parameter is structural rather than a named environment type. All this
needs is a possible LOG_LEVEL, SERVICE_NAME and LOG_FIELDS, so asking
for exactly that keeps the kernel free of any platform's environment shape
— a runtime's Env object, a process.env, or a bare object literal all
satisfy it.
The service name is read from the environment first and the configuration surface second. Keeping the environment value beside deployment settings lets a rename update both together, avoiding a stale name compiled into the bundle. The configuration value remains the fallback for runtimes with no environment to read.
With neither set the line still carries a service, reading unknown. A
missing name is a deployment defect, and an omitted key would make it an
invisible one — service = unknown is a query that finds every
misconfigured deployment.
Emits an info line.
What happened, as a dotted event name.
Optionaldata: Record<string, unknown>
Structured detail.
Optionalmessage: string
A sentence for someone reading the stream. Defaults to the event name.
Private StaticparseParses a level name, ignoring anything unrecognised.
Optionalvalue: string
The candidate level name.
The level, or null when the value is not one.
Private StaticserializeMakes a data payload serialisable, and safe to ship.
The payload to convert.
The payload scrubbed of secrets, with errors expanded to message and stack.
Two passes for two different problems. redact runs first, because a
credential must not survive into the line whatever shape it arrived in;
then Error expansion, which JSON.stringify would otherwise flatten to
{}, losing the only useful part of an error line.
The order is load-bearing in one direction only: redaction returns Error
values untouched precisely so this pass can still recognise them.
Emits a warn line.
What happened, as a dotted event name.
Optionaldata: Record<string, unknown>
Structured detail.
Optionalmessage: string
A sentence for someone reading the stream. Defaults to the event name.
Writes structured JSON through the console method matching each severity level.
Remarks
Use a stable, dotted event name such as
article.created. Put variable values in the payload and an optional human-readable sentence in the third argument. When omitted, the message defaults to the event name.Context and payload fields are flattened onto each line. The field allow-list filters context only; explicit payload fields remain available. Neither may overwrite
level,event, ormessage.LOG_LEVEL,SERVICE_NAME, andLOG_FIELDSoverride the registered logging settings. With no service name, lines useunknown. A child logger adds context while keeping its parent's threshold and field selection.Payloads pass through redaction, then top-level Error values expand to message and stack. The runtime controls log collection and retention.
Example
Author
Bayu Dwiyan Satria
Version
1.2.1
Since
1.0.0