# logger — Agent usage Configure a coding agent for @nexload-sdk/logger. **Topic:** agents **Package:** `@nexload-sdk/logger` v5.0.1 **Canonical page:** https://gecut.github.io/nexload-sdk/packages/logger/agents/ ## Install matching skills Install the skills whose triggers match the task. ### logger [Trigger](https://github.com/gecut/nexload-sdk/blob/main/skills/logger/SKILL.md) ```text MUST be loaded before writing, changing or reviewing ANY log call, console.* call, catch block, error handling, job/worker, outbound HTTP/provider call, route handler or observability code in a project that uses @nexload-sdk/logger. Decides whether a log is needed, its level, event name and fields, and what must never be logged. Do not use for metrics/health endpoints (use healthcheck skills). ``` ```bash npx skills add gecut/nexload-sdk --skill logger ``` ## Project AGENTS.md ```markdown Before adding, changing or reviewing any log call, console call, catch block, job/worker or outbound call, load the installed logger skill and follow its Decision flow. Use @nexload-sdk/logger for application and package logging. Never use console.* in package or app source. ``` ## Package context [Scoped Markdown bundle](/nexload-sdk/packages/logger/llms.txt). ## Prompts ```text Correlate a request with its log events. Use @nexload-sdk/logger and logger. Inspect the installed version and show a minimal typed example for my project. ``` ```text Review my @nexload-sdk/logger integration against the optimization page. Identify the relevant source for each finding and one test for the highest-risk behavior. ``` --- # API Reference & Compatibility Public logger signatures, examples, and runtime compatibility. **Topic:** api **Package:** `@nexload-sdk/logger` v5.0.1 **Canonical page:** https://gecut.github.io/nexload-sdk/packages/logger/api/ The inventory below is generated from the source entry points. The reference that follows describes the current source contract. ## Functions ### `configureLogger` ```ts configureLogger(options: LoggerOptions) => Logger ``` **Exported from:** `@nexload-sdk/logger` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/config.ts#L101) ### `createTestLogger` ```ts createTestLogger(options?: Partial) => { logger: Logger; entries: LogEntry[]; clear(): void; } ``` **Exported from:** `@nexload-sdk/logger/testing` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/testing.ts#L5) ### `getLogContext` ```ts getLogContext() => LogBindings ``` **Exported from:** `@nexload-sdk/logger` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/context.node.ts#L19) ### `getPayloadLoggerConfig` ```ts getPayloadLoggerConfig() => PayloadLoggerConfig ``` **Exported from:** `@nexload-sdk/logger` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/config.ts#L131) ### `resolveTraceId` ```ts resolveTraceId(headers: Headers | HeaderRecord) => string ``` **Exported from:** `@nexload-sdk/logger` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/trace.ts#L12) ### `traceHeaders` ```ts traceHeaders() => Record ``` **Exported from:** `@nexload-sdk/logger` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/trace.ts#L18) ### `withLogContext` ```ts withLogContext(bindings: LogBindings, fn: () => T) => T ``` **Exported from:** `@nexload-sdk/logger` Public function exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/context.node.ts#L15) ## Constants ### `DEFAULT_REDACT_PATHS` ```ts DEFAULT_REDACT_PATHS: readonly string[] ``` **Exported from:** `@nexload-sdk/logger` Public constant exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/redact.ts#L3) ### `TRACE_HEADER` ```ts TRACE_HEADER: "x-trace-id" ``` **Exported from:** `@nexload-sdk/logger` Public constant exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/trace.ts#L3) ### `logger` ```ts logger: Logger ``` **Exported from:** `@nexload-sdk/logger` Public constant exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/index.ts#L18) ## Interfaces ### `Logger` ```ts interface Logger { trace: LogMethod; debug: LogMethod; info: LogMethod; success: LogMethod; warn: LogMethod; error: LogMethod; fatal: LogMethod; child(bindings: LogBindings): Logger; isLevelEnabled(level: LogLevel): boolean; } ``` **Exported from:** `@nexload-sdk/logger` Public interface exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/types.ts#L22) ### `LoggerOptions` ```ts interface LoggerOptions { service: string; level?: LogLevel; release?: string; instance?: string; environment?: string; redact?: { paths?: string[]; replaceDefaults?: boolean; censor?: string }; pretty?: boolean; destination?: { write(line: string): void }; base?: Record; force?: boolean; } ``` **Exported from:** `@nexload-sdk/logger` Public interface exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/types.ts#L34) ## Types ### `LogBindings` ```ts type LogBindings = Record; ``` **Exported from:** `@nexload-sdk/logger` Public type exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/types.ts#L10) ### `LogEntry` ```ts type LogEntry = { level: LogLevel; time: string; service: string; msg?: string; [key: string]: unknown; }; ``` **Exported from:** `@nexload-sdk/logger` Public type exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/types.ts#L12) ### `LogLevel` ```ts type LogLevel = | "trace" | "debug" | "info" | "success" | "warn" | "error" | "fatal"; ``` **Exported from:** `@nexload-sdk/logger` Public type exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/types.ts#L1) ### `PayloadLoggerConfig` ```ts type PayloadLoggerConfig = { options: ReturnType; destination?: LoggerOptions["destination"]; }; ``` **Exported from:** `@nexload-sdk/logger` Public type exported by @nexload-sdk/logger. [Source](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/config.ts#L16) *** ## Exported API Summary Import the following from `@nexload-sdk/logger`. The testing helper has its own subpath. | Export | Signature | Example and behavior | |---|---|---| | `logger` | `const logger: Logger` | `import { logger } from "@nexload-sdk/logger"`. The lazy proxy resolves the current configuration on each call. | | Default export | `export default logger` | `import logger from "@nexload-sdk/logger"`. The default and named exports share the same proxy. | | `getPayloadLoggerConfig` | `getPayloadLoggerConfig(): { options: pino.LoggerOptions; destination?: { write(line: string): void } }` | Node.js Payload configuration bridge. Call `configureLogger()` before `buildConfig()`; rebuild Payload configuration after forced reconfiguration. | | `configureLogger` | `configureLogger(options: LoggerOptions): Logger` | `configureLogger({ service: "shop-api" })` configures the runtime. | | `withLogContext` | `withLogContext(bindings: LogBindings, fn: () => T): T` | `withLogContext({ requestId: "req-123" }, handleRequest)` runs the handler with request bindings where async context is supported. | | `getLogContext` | `getLogContext(): LogBindings` | `const { requestId } = getLogContext()` reads the current bindings. | | `TRACE_HEADER` | `const TRACE_HEADER: "x-trace-id"` | `response.headers.set(TRACE_HEADER, traceId)` sends the trace ID in the response. | | `resolveTraceId` | `resolveTraceId(headers: Headers \| Record): string` | `resolveTraceId(request.headers)` reuses an incoming ID matching `/^[A-Za-z0-9-]{8,64}$/` or generates a UUID. | | `traceHeaders` | `traceHeaders(): Record` | `const outbound = new Headers(traceHeaders())` copies the active trace header. With no active trace ID it returns an empty record. | | `DEFAULT_REDACT_PATHS` | `const DEFAULT_REDACT_PATHS: readonly string[]` | `const paths = [...DEFAULT_REDACT_PATHS, "customer.nationalCode"]` copies the defaults for inspection or reuse. | ### Public Types | Type | Signature | Example | |---|---|---| | `LogLevel` | `"trace" \| "debug" \| "info" \| "success" \| "warn" \| "error" \| "fatal"` | `const level: LogLevel = "warn"` | | `LogBindings` | `Record` | `const bindings: LogBindings = { module: "orders" }` | | `Logger` | See methods below. | `const log: Logger = logger.child({ module: "orders" })` | | `LoggerOptions` | See configuration signature below. | `const options: LoggerOptions = { service: "shop-api", level: "info" }` | | `LogEntry` | `{ level: LogLevel; time: string; service: string; msg?: string; [key: string]: unknown }` | `const first: LogEntry \| undefined = entries[0]` | All level methods use the same signature. ```ts interface Logger { trace(objOrMsg?: object | Error | string, msg?: string): void; debug(objOrMsg?: object | Error | string, msg?: string): void; info(objOrMsg?: object | Error | string, msg?: string): void; success(objOrMsg?: object | Error | string, msg?: string): void; warn(objOrMsg?: object | Error | string, msg?: string): void; error(objOrMsg?: object | Error | string, msg?: string): void; fatal(objOrMsg?: object | Error | string, msg?: string): void; child(bindings: LogBindings): Logger; isLevelEnabled(level: LogLevel): boolean; } ``` `log.isLevelEnabled("debug")` checks whether debug events will be emitted. Levels map to `trace: 10`, `debug: 20`, `info: 30`, `success: 35`, `warn: 40`, `error: 50`, and `fatal: 60`. ```ts interface LoggerOptions { service: string; level?: LogLevel; release?: string; instance?: string; environment?: string; redact?: { paths?: string[]; replaceDefaults?: boolean; censor?: string }; pretty?: boolean; destination?: { write(line: string): void }; base?: Record; force?: boolean; } ``` On Node, pretty output defaults to enabled outside production; `destination` takes precedence. Browser and Edge use pino browser output and ignore these server stream options. Server log records include `service`, `pid`, and ISO `time`, plus configured `release`, `instance`, and `environment` fields. ### Testing Subpath ```ts function createTestLogger(options?: Partial): { logger: Logger; entries: LogEntry[]; clear(): void; }; ``` ```ts import { createTestLogger } from "@nexload-sdk/logger/testing"; const { logger, entries, clear } = createTestLogger({ service: "test", level: "info" }); logger.success({ event: "order.lifecycle.placed", orderId: "123" }, "Order placed"); const event = entries[0]?.event; clear(); ``` The test logger captures parsed entries independently of global configuration. `clear()` empties the existing entries array. *** Root export conditions `browser`, `edge-light`, and `worker` select the browser bundle; `import` and `require` select Node bundles. The `./testing` subpath provides Node import and require bundles. Default redaction covers the ten sensitive keys listed in the [guide](../guides/) at the top level and one level below, plus the explicit header and body paths. It does not recursively redact deeper keys. The censor defaults to `[REDACTED]`. Error serialization includes type, a 500-character message, a matching uppercase code, at most two nested causes, and a stack outside production. ## Runtime Compatibility Matrix | Runtime | Logging | Request context | |---|---|---| | Node.js and Bun | Server output with configurable destination | Async request bindings | | Edge and browser | Same root API | Explicit bindings and trace headers | --- # Production Guides & Recipes Configure the logger per runtime and use request context, trace headers, redaction, and error serialization. **Topic:** guides **Package:** `@nexload-sdk/logger` v5.0.1 **Canonical page:** https://gecut.github.io/nexload-sdk/packages/logger/guides/ ## Configure Once per Runtime Call `configureLogger` in the Node.js or Bun service entry point. Configure a browser bundle separately at its startup if it needs custom settings. The lazy logger uses environment fallbacks before configuration. `SERVICE_NAME` defaults to `unknown`, `RELEASE` supplies the release, and `LOG_LEVEL` defaults to `info` in production and `debug` elsewhere. The configuration is stored once per JavaScript realm, under `globalThis[Symbol.for("@nexload-sdk/logger/state")]`. Every copy of the package in the same process shares it: the ESM build, the CJS build, and copies inlined into separate Next.js server bundles. One `configureLogger` call therefore applies to route handlers, server components and libraries alike. A different runtime (Edge, browser, a worker thread) has its own realm and needs its own call. A second configuration call returns the existing logger and emits a warning with event `logger.reconfigure.ignored`. Use `force: true` when you intentionally need to replace it. ### Next.js Instrumentation Configure the logger in `instrumentation.ts`. Each server runtime (Node.js, Edge) initializes its own logger when Next.js invokes `register`; inside one runtime every bundle sees the same configuration. ```ts export async function register() { const { configureLogger } = await import("@nexload-sdk/logger"); configureLogger({ service: "shop-web", release: process.env.RELEASE }); } ``` *** ## Request Context and Trace Propagation Resolve the trace ID at the request boundary and attach it to a request scope. Node's async context carries bindings through awaited calls. Nested scopes merge bindings, with inner values taking precedence. ```ts import { logger, resolveTraceId, traceHeaders, withLogContext } from "@nexload-sdk/logger"; const log = logger.child({ module: "inventory" }); export async function GET(request: Request) { const requestId = crypto.randomUUID(); const traceId = resolveTraceId(request.headers); return withLogContext({ requestId, traceId }, async () => { const startedAt = performance.now(); const upstream = await fetch("https://inventory.example.com/availability", { headers: traceHeaders(), }); log.info({ event: "inventory.request.completed", status: upstream.status, durationMs: performance.now() - startedAt, outcome: upstream.ok ? "success" : "failure", }, "Inventory request completed"); return upstream; }); } ``` ### Edge and Browser `withLogContext` calls the callback directly, and `getLogContext()` returns an empty object. Browser output uses pino browser records; server `pretty` and `destination` settings do not apply. Bind IDs with `logger.child({ requestId, traceId })` and forward the trace explicitly with `{ [TRACE_HEADER]: traceId }`. *** ## Redaction ```ts import { configureLogger } from "@nexload-sdk/logger"; configureLogger({ service: "shop-api", redact: { paths: ["customer.nationalCode"] }, }); ``` ### Path Coverage The default censor is `[REDACTED]`. Default key patterns cover `password`, `token`, `secret`, `apiKey`, `authorization`, `cookie`, `otp`, `phone`, `mobile`, and `merchant` at the top level and one level below it. The defaults also cover `req.headers.authorization`, `req.headers.cookie`, `headers.authorization`, `headers.cookie`, `body`, and `req.body`. Extra paths extend the defaults unless you set `replaceDefaults: true`. Deeper fields such as `user.profile.phone` require an explicit path. Keep log fields limited to IDs and safe metadata, and keep secrets out of message strings. *** ## Errors Pass the error under `err`, or pass an `Error` as the first argument. Log it once at the boundary that handles it. Let errors propagate to that boundary when the caller handles them. ```ts import { logger } from "@nexload-sdk/logger"; const log = logger.child({ module: "orders" }); export function orderFailure(err: unknown) { log.error({ err, event: "order.request.failed" }, "Order request failed"); return new Response("Unable to process order", { status: 500 }); } ``` ### Serialized Fields The serializer includes the error type, up to 500 characters of its message, an optional code that matches `/^[A-Z][A-Z0-9_]{1,99}$/`, and up to two nested causes (three error records including the root). Non-Error values become a type and string message. Stack traces are included outside production. *** ## Testing Use `createTestLogger` from `@nexload-sdk/logger/testing` to assert on emitted events. It captures parsed entries independently of the global configuration, so tests do not need to reset shared state. ```ts import { createTestLogger } from "@nexload-sdk/logger/testing"; const { logger, entries, clear } = createTestLogger({ service: "test", level: "info" }); logger.success({ event: "order.lifecycle.placed", orderId: "123" }, "Order placed"); entries[0]?.event; // "order.lifecycle.placed" clear(); ``` Inject the test logger into the code under test instead of mocking the shared `logger`. *** ## Libraries A library that logs imports the shared logger and never calls `configureLogger`. The host application owns configuration, so the library's module children follow it. Before the application configures the logger, calls use the environment fallbacks described above. *** ## Logging Architecture Create one child per module. Log on request completion, outbound provider calls, job completion, and business state transitions. Aggregate loop results and log the summary once. Give each event a stable past-tense `domain.entity.action` name and a short message. Add `durationMs` and `outcome` to timed operations, and `attempt` to retry events. Use `info` for lifecycle summaries, `success` for business milestones, `warn` for handled degradation, and `error` for unexpected failures. Reserve `fatal` for conditions that prevent the process from continuing. Use `debug` and `trace` for diagnostics. Skip logs for successful health checks. See the [logger skill](https://github.com/gecut/nexload-sdk/tree/main/skills/logger) for boundary, field, and level rules. *** ## Troubleshooting & Migration from v4 ### 1. Global Setup and Children's Bindings Replace `NexloadLogger` and `createGlobalLogger` with startup configuration and the shared `logger` export. Pass an object to `child`, such as `logger.child({ module: "orders" })`. ### 2. Renderers and Level Filters Replace custom renderer APIs with `pretty` or `destination`. Update structured-log queries and filters to use lowercase output labels; the Node pretty stream displays uppercase labels. Existing uppercase `LOG_LEVEL` values are accepted. ### 3. Tests and Error Fields Replace `@nexload-sdk/logger/log-test` with `createTestLogger` from `@nexload-sdk/logger/testing`. Review redaction coverage and pass errors under `err` or as the first argument. --- # Overview & Quick Start Configure-once structured logging with request context, trace propagation, and safe defaults. **Topic:** overview **Package:** `@nexload-sdk/logger` v5.0.1 **Canonical page:** https://gecut.github.io/nexload-sdk/packages/logger/ **Package:** `@nexload-sdk/logger` **Workspace version:** `5.0.1` Configure-once structured logger for Node, Edge and browser, built on pino, with safe defaults. [npm](https://www.npmjs.com/package/@nexload-sdk/logger) · [Source](https://github.com/gecut/nexload-sdk/tree/main/packages/logger) The logger implementation records structured events at service boundaries. Configure one logger per runtime, then use the shared logger or a module child throughout your application. *** ## 10-Second Code Snippet ```ts import { configureLogger, logger } from "@nexload-sdk/logger"; configureLogger({ service: "shop-api" }); const log = logger.child({ module: "orders" }); log.success({ event: "order.lifecycle.placed", orderId: "123" }, "Order placed"); ``` *** ## Installation & Peer Dependencies ```bash pnpm add @nexload-sdk/logger ``` ### Compatibility Requirements | Runtime / Dependency | Support | Notes | |---|---|---| | Node.js and Bun | Server logger | JSON lines to stdout in production, readable output by default in development | | Edge and browser | Same root API | Pass request identifiers explicitly | | pino | Bundled as a package dependency | No peer dependencies to install | *** ## When to Use Logger Use it for request summaries, outbound provider calls, job runs, process lifecycle events, and business milestones. Use the Healthcheck packages for probes and metrics. Use your platform's log collector for shipping stdout logs and a dedicated telemetry integration for distributed spans. *** ## Quick Start: Configure a Service Call `configureLogger` in your service entry point before accepting requests. Module children created before startup configuration follow the configured logger on subsequent calls. ```ts import { configureLogger } from "@nexload-sdk/logger"; configureLogger({ service: "shop-api", release: process.env.RELEASE }); ``` *** ## Next Steps * Explore [Production Guides & Recipes](./guides/) for Next.js startup, trace propagation, redaction, and migration. * Check the [API Reference & Compatibility](./api/) for the root and testing exports. ## Next steps [Optimization](./optimization/) [Agent usage](./agents/) --- # logger — Optimization Production choices for @nexload-sdk/logger. **Topic:** optimization **Package:** `@nexload-sdk/logger` v5.0.1 **Canonical page:** https://gecut.github.io/nexload-sdk/packages/logger/optimization/ ## Extend redaction for deeper objects Default redaction covers keys at the top level and one level below it. Add explicit paths for sensitive fields deeper in the object. ## Configure once Call `configureLogger()` at startup. A subsequent call without `force: true` keeps the existing configuration. ## Respect browser and Edge behavior Browser and Edge builds use Pino browser output. `withLogContext` is pass-through, and `pretty` and `destination` do not configure a server stream. ## Rebuild Payload configuration after replacement A forced logger reconfiguration requires rebuilding Payload configuration created with `getPayloadLoggerConfig()` to pick up the change. ## Sources [Package guidance](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/README.md) · [Implementation](https://github.com/gecut/nexload-sdk/blob/main/packages/logger/src/index.ts). · [Matching skill](https://github.com/gecut/nexload-sdk/blob/main/skills/logger/SKILL.md).