Create a trace flag for a user, Apex class, or Apex trigger.
Description for apex trace create
Use trace flags to set up debug logging for a specified entity (user, Apex class, or Apex trigger). You must specify an existing debug level and the entity to trace. The trace flag expires after the specified duration (default 30 minutes).
To find the list of existing debug levels, run this Tooling API query on your org using the "data query" CLI command: "sf data query --query "SELECT DeveloperName FROM DebugLevel" --use-tooling-api". To create a debug level in your org, see "Set Up Debug Logging" (https://help.salesforce.com/s/articleView?id=xcloud.code_add_users_debug_log.htm)
Use the --log-type flag to specify the type of debug log file to create; you have these two options:
<ul><li>DEVELOPER_LOG (default) — captures a full debug log, including Apex execution, SOQL, DML, callouts, and so on.</li><li>USER_DEBUG — captures only System.debug() statements and user-generated log lines. Use this option for lighter-weight smaller logs.</li></ul>After you create the trace flag, follow these steps to use it:
-
Perform the action that you want to debug. For example, if you created a trace flag for an Apex class, run the class.
-
Run the "apex log list" CLI command to get a list of the available debug logs in your org. Make note of the ID of the debug log you're interested in.
-
Run the "apex log get" command and specify this ID log with the --log-id flag. You can also use the --number flag to get the most recent debug logs.
-
Examine the debug log for information about the entity you created a trace flag for. See "Debug Log" (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_debugging_debug_log.htm) for more information.
Flags
| Flag Name (Long) | Flag Name (Short) | Description |
|---|---|---|
| <code>‑‑api‑version</code> | N/A | Type: Value<br><br>Override the api version used for api requests made by this command |
| <code>‑‑debug‑level</code> | <code>‑d</code> | Type: Value<br>Required<br><br>Developer name of an existing debug level to apply. |
| <code>‑‑duration</code> | N/A | Type: Value<br>Default value: 30<br><br>Duration, in minutes, before the trace flag expires. Maximum is 1440 (24 hours). |
| <code>‑‑flags‑dir</code> | N/A | Type: Value<br><br>Import flag values from a directory. |
| <code>‑‑json</code> | N/A | Type: Boolean<br><br>Format output as json. |
| <code>‑‑log‑type</code> | <code>‑l</code> | Type: Value<br>Valid Values: DEVELOPER_LOG, USER_DEBUG<br>Default value: DEVELOPER_LOG<br><br>Type of trace flag to create. |
| <code>‑‑target‑org</code> | <code>‑o</code> | Type: Value<br>Required<br><br>Username or alias of the target org. Not required if the target-org configuration variable is already set. |
| <code>‑‑traced‑entity‑id</code> | <code>‑i</code> | Type: Value<br>Required<br><br>ID of the user, Apex class, or Apex trigger to trace. |
Examples for apex trace create
Create a trace flag for a user (ID starts with 005) with the SFDC_DevConsole debug level in your default org:
sf apex trace create --traced-entity-id 005xx000001Svs8AAC --debug-level SFDC_DevConsole
Create a USER_DEBUG trace flag that lasts 60 minutes in the org with alias "my-org":
sf apex trace create --traced-entity-id 005xx000001Svs8AAC --debug-level MyDebugLevel --log-type USER_DEBUG --duration 60 --target-org my-org
<!-- prettier-ignore-end -->