apex trace create

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:

  • DEVELOPER_LOG (default) — captures a full debug log, including Apex execution, SOQL, DML, callouts, and so on.
  • USER_DEBUG — captures only System.debug() statements and user-generated log lines. Use this option for lighter-weight smaller logs.

After you create the trace flag, follow these steps to use it:

  1. Perform the action that you want to debug. For example, if you created a trace flag for an Apex class, run the class.

  2. 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.

  3. 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.

  4. 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
‑‑api‑versionN/AType: Value

Override the api version used for api requests made by this command
‑‑debug‑level‑dType: Value
Required

Developer name of an existing debug level to apply.
‑‑durationN/AType: Value
Default value: 30

Duration, in minutes, before the trace flag expires. Maximum is 1440 (24 hours).
‑‑flags‑dirN/AType: Value

Import flag values from a directory.
‑‑jsonN/AType: Boolean

Format output as json.
‑‑log‑type‑lType: Value
Valid Values: DEVELOPER_LOG, USER_DEBUG
Default value: DEVELOPER_LOG

Type of trace flag to create.
‑‑target‑org‑oType: Value
Required

Username or alias of the target org. Not required if the target-org configuration variable is already set.
‑‑traced‑entity‑id‑iType: Value
Required

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:

1sf 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”:

1sf apex trace create --traced-entity-id 005xx000001Svs8AAC --debug-level MyDebugLevel --log-type USER_DEBUG --duration 60 --target-org my-org