# Query Code Extension Logs by Using Salesforce CLI

Use Salesforce CLI to query logs generated for code extension runs and export them to a file. Logs are stored in the Data Lake Object (DLO) named `DataCustomCodeLogs__dll`, and you can query them like any other DLO. Salesforce CLI is useful when you want to read multiline messages, such as stack traces, or export results to a CSV file for offline analysis.

| Edition Table                                                                                                                                                                                                 |
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Available in: **Developer**, **Enterprise**, **Performance**, and **Unlimited** Editions. See Data 360 [edition availability](https://help.salesforce.com/s/articleView?id=sf.c360_a_dc_editions.htm&type=5). |

| User Permissions Needed                               |                                                        |
| :---------------------------------------------------- | :----------------------------------------------------- |
| To query code extension logs by using Salesforce CLI: | Permission set: <ul><li>Data Cloud Architect</li></ul> |

## Prerequisites

- Data 360 access with permission to query data.
- Install Salesforce CLI and authenticate to your target org. See [Set Up Salesforce CLI for Code Extension](/docs/data/data-cloud-code-ext/guide/set-up-sdk.md).
- Enable Code Extension in Data Cloud Setup by using the Feature Manager. See [Enable Data 360 Features in Feature Manager](https://help.salesforce.com/s/articleView?id=data.c360\_a_feature_manager.htm\&language=en_US\&type=5).
- A completed code extension execution.

## Considerations

- **Latency:** Logs typically appear within a few minutes after code execution. Allow up to about 10 minutes.
- **Message limits:** The maximum log message length is 31,072 characters. Longer messages are truncated.
- **Readability:** Multiline messages, such as stack traces, are easier to read in Salesforce CLI output or in an exported CSV file.

## Steps to Query Code Extension Logs by Using Salesforce CLI

1. Understand the relevant fields of the `DataCustomCodeLogs__dll` DLO.

   | Field (API Name)           | Description                                                                                   | Data type |
   | :------------------------- | :-------------------------------------------------------------------------------------------- | :-------- |
   | `EventId__c`               | DLO primary key for each log record                                                           | UUID      |
   | `Timestamp__c`             | Log record timestamp                                                                          | Datetime  |
   | `Message__c`               | Your log message                                                                              | Text      |
   | `CorrelationId__c`         | Correlates your code extension logs to Data 360 internal logs                                 | UUID      |
   | `ExecutionId__c`           | Unique identifier for the code execution; multiple `CorrelationId__c` can share one execution | UUID      |
   | `DataCustomCodeName__c`    | Code extension package name                                                                   | Text      |
   | `ProcessDefinitionName__c` | Name of the batch data transform or search index that uses the code package                   | Text      |
   | `UsedInFeature__c`         | Feature where the code is used, for example, batch data transform or search index             | Text      |
   | `OrgId__c`                 | Org identifier                                                                                | Text      |

2. Confirm that you're signed in to your target org.

   ```bash
   sf org display --target-org <ORG_ALIAS>
   ```

3. Query the `DataCustomCodeLogs__dll` DLO with the `sf data query` command.

   ```bash
   sf data query --target-org <ORG_ALIAS> --query "SELECT Timestamp__c, DataCustomCodeName__c, Message__c, ExecutionId__c, EventId__c, CorrelationId__c FROM DataCustomCodeLogs__dll ORDER BY Timestamp__c DESC LIMIT 1000"
   ```

4. To export the results to a CSV file, add `--result-format csv` and redirect the output to a file. A CSV file preserves multiline messages, such as stack traces, in a readable format.

   ```bash
   sf data query --target-org <ORG_ALIAS> --query "SELECT Timestamp__c, DataCustomCodeName__c, Message__c, ExecutionId__c, EventId__c, CorrelationId__c FROM DataCustomCodeLogs__dll ORDER BY Timestamp__c DESC LIMIT 1000" --result-format csv > custom_code_logs.csv
   ```

## Example Queries

- Filter by transform (process) name.

```bash
sf data query --target-org <ORG_ALIAS> --query "SELECT Timestamp__c, Message__c FROM DataCustomCodeLogs__dll WHERE ProcessDefinitionName__c = 'MyTransformName' ORDER BY Timestamp__c DESC LIMIT 100"
```

- Filter by code extension package name.

```bash
sf data query --target-org <ORG_ALIAS> --query "SELECT Timestamp__c, Message__c FROM DataCustomCodeLogs__dll WHERE DataCustomCodeName__c = 'my_custom_code' ORDER BY Timestamp__c DESC LIMIT 100"
```

- Filter by execution ID.

```bash
sf data query --target-org <ORG_ALIAS> --query "SELECT Timestamp__c, Message__c FROM DataCustomCodeLogs__dll WHERE ExecutionId__c = '00000000-0000-0000-0000-000000000000' ORDER BY Timestamp__c DESC"
```

## Inspect Object Fields for `config.json`

You can also use Salesforce CLI to describe an object and list its fields. This capability is helpful when the target data model object (DMO) already exists and you're building the output schema in your `config.json`. You can see the exact field names and types to include.

The `sf sobject describe --json` command returns the object's complete metadata, which is lengthy. To show only each field's name, type, and label, the examples pipe the output to `jq`, a command-line JSON processor. `jq` is a separate tool that isn't included with Salesforce CLI. If you don't have `jq`, run the command without the `| jq ...` part to see the full response.

- Describe the logs DLO.

```bash
sf sobject describe --target-org <ORG_ALIAS> --sobject DataCustomCodeLogs__dll --json | jq '.result.fields[] | {name: .name, type: .type, label: .label}'
```

- Describe a DMO to view the fields for your `config.json` output schema.

```bash
sf sobject describe --target-org <ORG_ALIAS> --sobject <DMO_API_NAME> --json | jq '.result.fields[] | {name: .name, type: .type, label: .label}'
```

## See Also

- [Query Code Extension Logs by Using Query Editor](/docs/data/data-cloud-code-ext/guide/query-logs-queryeditor.md)
- [Query Code Extension Logs by Using the Query API](/docs/data/data-cloud-code-ext/guide/query-logs-api.md)
