# View SCAPI Logs and Track Requests

Log Center provides comprehensive logging and monitoring capabilities for B2C Commerce API (SCAPI) operations. Use Log Center to troubleshoot issues, monitor performance, and analyze API behavior across your B2C Commerce instances.

:::note
You can use the B2C Commerce CLI to interface with B2C logging systems to tail logs or fetch specific request IDs. First, see [B2C CLI, MCP and Tooling SDK](/docs/commerce/b2c-commerce/guide/b2c-developer-tooling.html) to install the tool and authenticate.

Once configured, use the CLI to retrieve log information:

```bash
# Tail the error logs for your instance in real-time
b2c logs tail --filter error
```

For a complete list of logging CLI commands, see [Logs Commands](https://salesforcecommercecloud.github.io/b2c-developer-tooling/cli/logs.html).
:::

## Access Log Center

1. Log in to Business Manager.
2. Click App Launcher ![App Launcher](https://a.sfdcstatic.com/developer-website/sfdocs/commerce-cloud/media/shared/appLauncher.png) and then select **Administration** > **Site Development** > **Development Setup**. If you built your site using Progressive Web App (PWA) Kit, first complete the prerequisites described in [Debug Using Log Center](/docs/commerce/b2c-commerce/guide/debugging.html#debug-using-log-center).

For general information on Log Center, see [Centralized Log Center](https://help.salesforce.com/s/articleView?id=cc.b2c_log_center.htm\&type=5).

## Search for SCAPI Logs

In Log Center, navigate to **Search**.

:::note
SCAPI logs appear in Log Center with a delay of up to 5 minutes.
:::

### Investigate By Correlation ID

You can use correlation IDs to match a particular request to a particular response, making it easier to troubleshoot unexpected issues, such as internal server errors.

If a `correlation-id` header is provided in a request to a SCAPI endpoint, the response contains an `correlation-id` header that contains the value of `correlation-id`. The API also generates its own correlation ID for each response. This generated ID is provided in the `sfdc_correlation_id` response header.

Example headers:

```bash
# Request
correlation-id: 40e1feef-c454-49cd-9abc-cc995e1f960e

# Response header - DEPRECATED, will be removed
x-correlation-id: 40e1feef-c454-49cd-9abc-cc995e1f960e,7672402e9add1b37

# Response header - replacement for x-correlation-id
sfdc_correlation_id: 7672402e9add1b37
```

The value of `correlation-id` must only contain word characters, hyphens (-), and commas (,). Other characters may prevent you from searching for that correlation ID string in Log Center.

For most Commerce API endpoints, the `correlation-id` can be automatically added to log messages with a hook. Query these messages in Log Center with an LCQL (Log Center Query Language) expression that filters messages based on the `externalID` field. The `externalID` field contains the value of the `correlation-id` response header, so query for the user-provided correlation ID, the SCAPI-generated correlation ID, or the combined string.

If you can reproduce an issue, you might be able to identify the problem either by attaching your own correlation ID or by observing the automatically generated correlation ID in the response.

Use this correlation ID to trace specific requests:

1. In Log Center search, enter the correlation ID in the search field or use a Log Center Query Language (LCQL) expression on the `externalID` field, for example: `externalID:(9527f1869f95a32d)`.
2. Filter by relevant service types for more focused results, which is only recommended if the result set is very large.

```javascript
const Logger = require("dw/system/Logger");

exports.beforeGET = function () {
  // When you log, correlation IDs are automatically associated with your message, so you don't need to provide the correlation ID to the log function.
  Logger.log("Hello!");
  return new Status(Status.OK);
};
```

:::note
Some groups of Commerce API endpoints are not used exclusively with B2C Commerce and therefore can't log requests and responses to Log Center. These API groups include CDN Zones, Inventory Availability, Inventory Impex, Inventory Reservation, Shopper Context, and Shopper Login.
:::

### Identify Error Patterns

You can also use the SCAPI logs in Log Center to investigate patterns of frequent errors:

1. Select the SCAPI category or categories from the dropdown menu.
   - Select `scapi-http` to identify requests with specific error response codes.
   - Select `scapi` to find error messages about how the SCAPI requests are processed, for example: incorrect requests, failing authentication, or routing issues.
2. Execute a search to identify error status types that occur.
3. To dig deeper into a specific error, obtain one or multiple correlation IDs from the results and search for further entries with the same correlation ID. For details, see [Investigating By Correlation ID](#investigate-by-correlation-id).

## Collect Detailed Request Information

Use SCAPI verbose logging to investigate errors, long latencies, hook execution errors, and remote service call errors within a single request context. Enable verbose logging in two ways:

- **Automatic setup** - Configure conditions that automatically trigger verbose logging (recommended for production environments where code changes are difficult).
- **Request header** - Add the `sfdc_verbose` header to individual requests (recommended for development environments where flexibility is needed).

The header method always takes precedence over automatic settings.

### Enable Verbose Logging with a Header

With B2C Commerce version 24.7, generate a JSON document that contains comprehensive information about the request. This JSON document is beneficial for troubleshooting, because it provides detailed information that is not included with standard logging, such as request authorization, hook execution, request query parameters, headers, and body.

:::important
To mitigate potential performance issues, the usage of the header is subject to rate limiting. Headers should only be used for individual test requests when a detailed investigation is necessary.
:::

To enable this feature, include the following header in your request:

- **Header:** `sfdc_verbose: true`

Upon the completion of the request, a JSON document is created and logged within the INFO log associated with the [correlation ID](#investigate-by-correlation-id) of that request.

Additionally, a request with the `sfdc_verbose` header creates `scapi-http` log entries in Log Center for all responses, while only error logs are available for non-verbose requests.

In combination with the `correlation-id`, the `sfdc_verbose` header can be used to generate and find verbose log output in the Log Center.

### Enable Automatic Verbose Logging

With B2C Commerce version 26.6, verbose logging has been enhanced to always track verbose logs automatically for a selected set of conditions.

Verbose logging automatically captures logs when:

This feature is enabled via a feature toggle from SFCC during rollout and remains enabled.

- SCAPI requests that call 3rd parties via remote service calls which return errors
- SCAPI requests ending with 4xx/5xx responses caused by hook execution errors

This feature is enabled via a feature toggle from SFCC during rollout and remains enabled.

Once a condition described above is met, a JSON document is created and logged within the INFO log associated with the [correlation ID](https://developer.salesforce.com/docs/commerce/commerce-api/guide/scapi-logs-request-tracking.md#investigate-by-correlation-id) of that request.

### Find Verbose Log Entries

1. Log in to Log Center as described in [Start Log Center](https://help.salesforce.com/s/articleView?id=cc.b2c_start_log_center.htm\&type=5).
2. Use the [correlation ID](https://developer.salesforce.com/docs/commerce/commerce-api/guide/identifying-requests-and-responses.html) or the category `scapi.verbose` in combination with the [Log Center Search](https://help.salesforce.com/s/articleView?id=cc.b2c_log_center_search.htm\&type=5) to find the corresponding log entry.

### Verbose Log Entry Schema

The following example shows the objects (`auth`, `request`, `response`, `customApi`, `hooks`, `services`) that can be present in a verbose log entry:

```json
{
  "auth": {
    "clientId": "<client-id>",
    "customer": {
      "customerId": "<customer-id>",
      "customerIdp": "slas",
      "customerLogin": "Guest",
      "customerType": "guest",
      "sessionBridgeToken": false
    },
    "grantedScopes": ["c_loyaltyInfo_rw"],
    "tokenType": "ShopperToken",
    "user": {
      "agentOnBehalfOfUser": "<user-id>",
      "taobToken": false
    }
  },
  "request": {
    "encodedQueryString": "siteId=RefArch",
    "headers": {
      "sfdc_verbose": "true"
    },
    "httpMethod": "GET",
    "queryParameters": {
      "siteId": "TestWapi-Customization",
      "c_responseHeader": "correct"
    },
    "requestUrl": "<request-url>",
    "runtimeInMillis": 124,
    "type": "on-request"
  },
  "response": {
    "headers": {
      "c_ResponseHeader": "test"
    },
    "httpStatus": "OK",
    "httpStatusCode": 200
  },
  "customApi": {
    "implementation": "test_bc_wapi/cartridge/rest-apis/sample/Sample.js#sampleFunction",
    "locale": "default",
    "mapping": "test_bc_wapi/cartridge/rest-apis/sample/api.json",
    "runtimeInMillis": 8
  },
  "hooks": [
    {
      "name": "dw.ocapi.shop.store.beforeGET",
      "result": "OK",
      "runtimeInMillis": 10,
      "scripts": ["test_bc_wapi/cartridge/scripts/store_shop_script.js"]
    },
    {
      "name": "dw.ocapi.shop.store.modifyGETResponse",
      "result": "OK",
      "runtimeInMillis": 4,
      "scripts": ["test_bc_wapi/cartridge/scripts/store_shop_script.js"]
    }
  ],
  "services": [
    {
      "name": "test.payment.service",
      "result": "OK",
      "runtimeInMillis": 123
    },
    {
      "name": "test.inventory.service",
      "result": "OK",
      "runtimeInMillis": 192
    }
  ]
}
```

## Review SCAPI Metrics

You can view SCAPI and SCAPI hook metrics in the Metrics Dashboard. To view the metrics, click **Metrics** in Log Center, and then click **SCAPI** or **SCAPI Hooks**. See [Metrics Dashboard](https://help.salesforce.com/s/articleView?id=cc.b2c_metrics_dashboard.htm\&type=5).

Additional SCAPI metrics are provided in the [CCAC SCAPI Dashboard](https://developer.salesforce.com/docs/commerce/b2c-commerce/guide/b2c-troubleshooting-platform-performance.html)

For additional details, see [Log Center Metrics Documentation](https://help.salesforce.com/s/articleView?id=cc.b2c_log_center.htm\&type=5).

## Related Resources

- [Log Center Search Documentation](https://help.salesforce.com/s/articleView?id=cc.b2c_log_center_search.htm\&type=5)
- [CCAC SCAPI Dashboard](https://developer.salesforce.com/docs/commerce/b2c-commerce/guide/b2c-troubleshooting-platform-performance.html)
- [Monitor SCAPI API performance](/docs/commerce/b2c-commerce/guide/code-profiler.html)
- [Analyze response bodies to identify maintenance issues](/docs/commerce/b2c-commerce/guide/response-handling.html)
- [Check error response codes](/docs/commerce/b2c-commerce/guide/error-response-codes.html)
