---
schema_version: "1"
id: 547cd45d82dc021e3cd3fb40d429a04ae6b835a96acd621300ff1d6ae0b52846
title: Logging in Storefront Next
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/commerce-cloud/sfnext/guides/sfnext-logging.md
canonical_url: https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/sfnext-logging.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 60b8d88e72307929f8425cafbf9065f47048d7e74e26af2e3e0e9814cfd5fc7f
products:
  - Composable Storefront
  - Salesforce APIs
---
# 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.

```typescript
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.

```typescript
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.

```typescript
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](https://runtime.commercecloud.com).
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](https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/references/mrt-admin?meta=projects_target_partial_update) with `"log_level"` in the payload.

**For local development**, use the `SFCC_LOG_LEVEL` environment variable:

```bash
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:

```json
{
  "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](https://help.salesforce.com/s/articleView?id=cc.b2c_log_center.htm&type=5) 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](https://account.demandware.com/). 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](https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/sfnext-mrt-administration.md#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](https://help.salesforce.com/s/articleView?id=cc.b2c_start_log_center.htm&type=5).
2. Select a **Region** and **[Realm](https://help.salesforce.com/s/articleView?id=cc.b2c_platform_overview.htm&type=5)**.
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](https://help.salesforce.com/s/articleView?id=cc.b2c_configuration_for_administrators.htm&type=5).

## See Also

- [Build Tools](https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/sfnext-build-tools.md)
