Debugging Salesforce using logs
- October 25, 2021
Getting bugs while developing is common, but reviewing and debugging them takes time. In this article, we’ll learn how to visualize Salesforce logs.
What is a trace flag?
Before moving forward with debug logs, we need to understand what a trace flag is and how we set it up.
A trace flag helps Salesforce trace any execution performed by a user and helps generate logs. Each trace flag includes a debug level, start time, end time and log type.
How to set up a trace flag
- Go to setup.
- In the Quick Find, search Debug Logs and click it.
- In the User Trace Flags section, click New.
- In the Trace Entity Name lookup, choose a User. And select a Start Date, Expiration Date, choose a Debug Level, or create one.
- Hit the Save button.
Note: Trace Entity Type can be of User, Class, Trigger, Automated Process or Platform Integration, depending upon your use case.
Debug levels
A trace flag includes a debug level, a start time, an end time and a log type. A debug level is a set of log levels for debug log categories, such as Database, Workflow and Validation. The log types are DEVELOPER_LOG, USER_DEBUG and CLASS_TRACING.
Log Categories | Details |
Database | DML statement or inline SOQL or SOSL query. |
System | Information about System Methods calls like System.debug() method. |
Apex Code | Includes information about Apex code. Can include information such as log messages generated by DML statements, inline SOQL or SOSL queries, the start and completion of any triggers, and the start and completion of any test method. |
Workflow | Includes information for workflow rules, flows, and processes, such as the rule name and the actions taken. |
Validation | Includes information about validation rules, such as the name of the rule and whether the rule evaluated true or false. |
Callout | Includes the request-response XML that the server is sending and receiving from an external web service. Useful when debugging issues related to using Lightning Platform web service API calls or troubleshooting user access to external objects via Salesforce Connect. |
Apex Profiling | Includes cumulative profiling information, such as the limits for your namespace and the number of emails sent. |
Visualforce | Includes information about Visualforce events, including serialization and deserialization of the view state or the evaluation of a formula field in a Visualforce page. |
NBA | Includes information about Einstein Next Best Action activity, including strategy execution details from Strategy Builder. |
Log level
Each debug level includes one of the following log levels for each log category. The levels are listed from lowest to highest. Specific events are logged based on the combination of category and levels. Most events start being logged at the INFO level. The level is cumulative, that is, if you select FINE, the log also includes all events logged at the DEBUG, INFO, WARN and ERROR levels.
- NONE
- ERROR
- WARN
- INFO
- DEBUG
- FINE
- FINER
- FINEST
Let’s say you want to have FINER info about your System.log (‘Your Debug Statement’), but your default level for System Category is set up to WARN, so you can override it by using:
System.debug(LoggingLevel.FINER, ‘Your Debug Statement’);
Debug logs
Debug logs are used to track the information that happens in your org. Logs can contain data about different log categories listed in the above section.
You can use debug logs to analyze your call stack, errors, performance issues and many other things.
You can choose different perspectives in the debug log to see different detailed sections.
Structure of a debug log
- The first line in Debug log is Header and it defines information about your Version Information and Log Categories with their respective Log Levels. 51.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
- The second line is about User Information. 21:32:55.0 (576594)|USER_INFO|[EXTERNAL]|0055g00000BQ0w7|vishalacme@apisero.com|(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)|GMT-07:00 21:32:55.0 (649313)|EXECUTION_STARTED
- From the third line on it shows information about your Execution. 21:32:55.0 (649313)|EXECUTION_STARTED
- Last line displays that the Execution has ended. 21:33:07.11 (12022715735)|EXECUTION_FINISHED
A debug log line consists of Timestamp (Number of Nanoseconds since the transaction started)+ Event Identifier.
Setup audit trail
Setup Audit Trails helps us analyze all the changes made into the org, be it creating a user, creating a field or modifying apex classes and processes.
One of the great use cases is to see the changes or track the details of changes that have been made in the org when you are working in a multi-user environment.
Setup Audit logs have data only up to the last six months. To see setup audit trails, go to setup and search View Setup Audit Trail.
Email logs
Email logs help review all the emails sent from the salesforce org and check the status for it, like whether that was successful or not.
To get the emails logs:
- Go to setup and in Quick Find, search for Emails Logs Files.
- Click Request an Email Log.
Email logs will only store the last 30 days’ emails details.
— By Vishal Lala