Core - v1.3.2
    Preparing search index...

    Class Logger

    Writes structured JSON through the console method matching each severity level.

    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, or message.

    LOG_LEVEL, SERVICE_NAME, and LOG_FIELDS override the registered logging settings. With no service name, lines use unknown. 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.

    const log = Logger.fromEnv(env, { component: 'ArticleService' })

    log.info('article.created', { id })
    log.info('article.created', { id }, `Article ${id} created`)

    Bayu Dwiyan Satria

    1.2.1

    1.0.0

    Index
    context: LogContext

    Fields repeated on every line.

    fields: LogFields

    Which of those fields the deployment actually wants ingested.

    threshold: number

    Minimum weight a line must carry to be emitted.

    • Assembles the object that becomes the line.

      Parameters

      • level: LogLevel

        Severity of the line.

      • event: string

        What happened, as a dotted event name.

      • Optionaldata: Record<string, unknown>

        Structured detail.

      • Optionalmessage: string

        A sentence for someone reading the stream.

      Returns Record<string, unknown>

      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.

      Parameters

      • event: string

        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.

      Returns void

    • Writes one line through the console method matching its level.

      Parameters

      • level: LogLevel

        Severity of the line.

      • event: string

        What happened, as a dotted event name.

      • Optionaldata: Record<string, unknown>

        Structured detail.

      • Optionalmessage: string

        A sentence for someone reading the stream.

      Returns void

      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.

      Parameters

      • event: string

        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.

      Returns void

    • Builds a logger for the current environment.

      Parameters

      • env: { LOG_FIELDS?: string; LOG_LEVEL?: string; SERVICE_NAME?: string }

        Anything carrying an optional LOG_LEVEL, SERVICE_NAME and LOG_FIELDS. Tolerates null.

      • context: LogContext = {}

        Fields to repeat on every line.

      Returns Logger

      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.

      Parameters

      • event: string

        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.

      Returns void

    • Parses a level name, ignoring anything unrecognised.

      Parameters

      • Optionalvalue: string

        The candidate level name.

      Returns LogLevel

      The level, or null when the value is not one.

    • Makes a data payload serialisable, and safe to ship.

      Parameters

      • data: Record<string, unknown>

        The payload to convert.

      Returns Record<string, unknown>

      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.

      Parameters

      • event: string

        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.

      Returns void