I have been trying to use Visual Studio code over the Developer Console because I understand that this will be the new standard for Salesforce however I find myself using the Developer Console with Visual Studio Code. The reason I use the DC with the VS Code is because while I am writing the code for the business processes I heavily use the Logs in DC with System Debug statements. My question is can VS Code also show the Logs like the DC? I get that you can create test classes and run it that way but I find using this is rather challenging when programming.
3 Answers
You can actually stream the debug log into your local computer with VSCode + Salesforce CLI tool.
Assuming you have the Salesforce CLI tool installed. To see debug log without leaving your VSCode, run this command in the terminal tab:
$ sfdx force:apex:log:tail -c | grep '|USER_DEBUG|'
Then, try create a new file within VSCode, type in below
System.debug('Hey!');
and run SFDX: Execute Anonymous Apex with Editor Contents
. You'll see the log is streamed to your console.
Check out the documentation.
-
awesome thank you. I looked for this before but all i found was you needed to create a test class first. Jun 24, 2020 at 15:05
-
-
1From the doc: by default it tails for 30 mins. But you can exit manually: Ctrl + C (Win) Jun 24, 2020 at 15:19
You can retrieve recent logs with the SFDX: Get Apex Debug Logs...
command from the Command Palette, and from there, you can use the Interactive Debugger commands on the log to step through your logic as if it were running in real-time. Many of the commands are not exposed through menu clicks, so using the Command Palette is the preferred way to get at all the available commands.
As a slightly different answer that does require more setup: there's a good trailhead about debugging your code in VS with checkpoints and the Apex Debugger which presents the ApexDebuggerEvent information through the Streaming API.
I've taken the below information from the trailhead and developer documentation.
First step, you need to create a permission set and assign it to your user record to give system permission "Debug Apex". Once that's assigned to your user, you can set up Apex Debugger:
Click on bug icon on left side bar
- Select
create a launch.json file
. - Select "Apex Debugger"
- This should open a launch.json file that contains at least the following (should auto-populate):
"configurations": [
{
"name": "Launch Apex Debugger",
"type": "apex",
"request": "launch",
"sfdxProject": "${workspaceRoot}"
}
]
- Save this file
Now, you can "launch apex debugger" by clicking on that same icon on the left sidebar.
It'll be actively streaming for information and the information will appear in the "Debug console" in the lower half next to "terminal". You can set checkpoints in your code to assist further.