Sub topic 11
Running anonymous Apex and collecting debug logs
This chapter assumes you have a DX project with an authorized default org (see the chapters on creating a project and authorizing an org). It also assumes GreetingService is deployed to that org. You will run a snippet of Apex without creating a class, then collect the debug log it produces. The next chapter replays that log in the Apex Replay Debugger.
What anonymous Apex is
Anonymous Apex is code that the org compiles and runs once, without saving it as a class or trigger. It runs in the context of your user and uses the code already deployed to the org. It is useful for quick experiments, data fixes, and for generating a debug log to inspect.
Every execution can produce a debug log. A debug log is a text record of what happened during a transaction: the statements run, System.debug output, limits used, and errors.
Write the snippet
Create a file named scripts/apex/hello.apex in your project. The .apex extension marks it as an anonymous Apex file. Add this code:
System.debug(GreetingService.greet('Ada'));This calls the class you deployed earlier. If the class is not in the org, the run fails with a compile error, so deploy first.
Run it from VS Code
- Open
hello.apex. - Open the Command Palette and run SFDX: Execute Anonymous Apex with Currently Selected Text to run a selection. Run SFDX: Execute Anonymous Apex with Editor Contents to run the whole file.
- Watch the Output panel. It shows the compile result and the debug log.
You can also run the file from the terminal with sf apex run --file scripts/apex/hello.apex.
Read the log
A log is made of lines, each with a timestamp and an event type. These events matter most when you start out:
| Event | What it tells you |
|---|---|
| `USER_DEBUG` | Output from your `System.debug` calls |
| `EXECUTION_STARTED` and `EXECUTION_FINISHED` | The boundaries of the run |
| `EXCEPTION_THROWN` | An unhandled or caught exception |
| `CUMULATIVE_LIMIT_USAGE` | How many governor limits the transaction used |
For the snippet above, look for a USER_DEBUG line that contains Hello, Ada!.
Control how much is logged
The amount of detail comes from the log levels in a trace flag. A trace flag tells the org which user to log, at what levels, and until when. A debug level sets the verbosity for each category, such as Apex Code or Database.
The extensions create these for you when you run anonymous Apex. If you need a log for a different user, or for activity that is not started from VS Code, run SFDX: Turn On Apex Debug Log for Replay Debugger. It creates a trace flag for your user with a debug level suited to replay. Turn it off when you finish, with SFDX: Turn Off Apex Debug Log for Replay Debugger.
Get logs you did not create
Use SFDX: Get Apex Debug Logs to list recent logs in the org and download one. It saves the file under .sfdx/tools/debug/logs, which keeps it out of your source folder. From the terminal, you can do the same with:
sf apex list log
sf apex get log --number 1Common errors
- Compile error on
GreetingService: the class is not deployed to the target org. Run the deploy step again. - No default org: the command fails until you authorize an org and set it as the default.
- No
USER_DEBUGlines: the Apex Code log level is belowDEBUG, or the trace flag expired. - Empty or truncated log: the 20 MB cap was reached. Lower the verbosity.
Exercise
You run an anonymous Apex file that calls GreetingService.greet('Ada'). The run reports a compile error that GreetingService does not exist. What is the most likely cause, and what do you do?
Show answer
The class is not in the org you ran against, because anonymous Apex uses only the code already deployed there. Deploy the project source to that org (for example with sf project deploy start --source-dir force-app), confirm the default org is the one you intended, and run the file again.
Exercise
Which command downloads the most recent debug log from the terminal, and where does the editor command save logs it downloads?
Show answer
sf apex get log --number 1 downloads the most recent log. The SFDX: Get Apex Debug Logs command saves logs under .sfdx/tools/debug/logs.