Logging in Storefront Next

Storefront Next includes a built-in structured logger that outputs JSON to stdout. The logger automatically enriches each log entry with a correlation ID and request context, making requests easy to trace across your application and search in Log Center.

Use the Logger

Server-Side (Loaders, Actions, Middleware)

Use getLogger(context) to get a request-scoped logger. It automatically includes the correlation ID, HTTP method, and path in every log entry.

import { getLogger } from "@/lib/logger.server";

export async function loader({ context }: LoaderFunctionArgs) {
  const logger = getLogger(context);
  logger.info("Product loaded", { productId: "sku-123" });
  logger.warn("Inventory low", { remaining: 2 });
  logger.error("Payment failed", { error: new Error("timeout") });
}

Client-Side or Without Context

Use createLogger() when router context isn't available, such as in client-side code or utility functions.

import { createLogger } from "@/lib/logger";

const logger = createLogger();
logger.info("Variant selected", { sku: "var-456" });

Metadata

Pass an optional metadata object as the second argument to any log method. Metadata fields are included as JSON properties in the log output.

logger.info("Order placed", {
  orderId: "order-789",
  total: 149.99,
  itemCount: 3,
});

The logger automatically serializes error instances with name, message, and stack fields.

Log Levels

Storefront Next supports four log levels, from most to least severe:

Level Use for
error Unrecoverable failures within a request
warn Recoverable problems: fallback paths, retries, degraded behavior
info Observable outcomes: request completed, action succeeded/failed, state changes
debug Internal progress: intermediate steps, useful for local debugging

Configure the Log Level

On Managed Runtime, configure the log level in your environment settings:

  1. Log in to Runtime Admin.
  2. Click your project, then your target environment.
  3. Click Environment Settings.
  4. In the Advanced section, set the Log Level.
  5. Click Update and wait for the bundle to redeploy.

Alternatively, set the log level via the Managed Runtime API. Call projects_target_partial_update with "log_level" in the payload.

For local development, use the SFCC_LOG_LEVEL environment variable:

SFCC_LOG_LEVEL=debug pnpm dev
Value Logs emitted
error error only
warn error + warn
info error + warn + info
debug all messages
(not set) warn in production, info otherwise

Log Format

In production, logs are output as structured JSON (ndjson). Each entry contains:

Field Description
level Log severity: error, warn, info, or debug
msg The log message
correlationId A unique ID for tracing a request across log entries
method HTTP method (GET, POST, etc.)
path URL pathname of the request

Example:

{
  "level": "info",
  "correlationId": "a1b2c3d4",
  "method": "GET",
  "path": "/products/shoes",
  "msg": "Product loaded",
  "productId": "sku-123"
}

In development, logs are pretty-printed with colors and timestamps for readability.

Correlation ID

Every incoming request is assigned a correlation ID — either extracted from the x-correlation-id request header or generated automatically. All log entries for that request share the same correlationId, which lets you trace a single request end to end.

View Logs in Log Center

Log Center lets you search and filter production logs from Managed Runtime and your B2C Commerce instance.

Prerequisites

To access MRT logs in Log Center:

  1. Have the Log Center User role in Account Manager. If you don't have the role, ask your Account Manager admin to give you access.
  2. Enable Log Center logs and Log Center metrics for the environment. See Log Center Logs and Metrics.

note: MRT logs in Log Center are available for any environment with Log Center logs enabled and a B2C Commerce instance associated. Marking the environment as production isn't required. There can be a delay of up to 15 minutes between an event and the log appearing in Log Center.

Access MRT Logs

  1. Start Log Center.
  2. Select a Region and Realm.
  3. Click Show filtered results.
  4. Click Search > Current Search.
  5. Under Service Type, select mrt.
  6. Under Request Type, select SSR for application logs or CDN for CDN logs.
  7. Under Host, select your environment ($PROJECT.$ENVIRONMENT).

Use the correlationId field to filter all log entries for a single request.

Limitations

  • Each MRT log message is limited to 32 KB. Characters beyond 32 KB are truncated.
  • For information about log levels and log volumes, see Configuration for Administrators.

See Also