Salesforce Debug Logs

A pilot relies on a flight recorder to understand what happened during a trip. Debug logs serve the same purpose in Salesforce. A log records the steps of a transaction: which code ran, which queries executed, which automation fired, and where an error occurred. Developers and admins read logs to solve problems that no screen message explains.

What a Debug Log Captures

  • Apex code execution, line by line.
  • SOQL queries and the number of rows returned.
  • DML operations and their results.
  • Flow and workflow actions.
  • Validation rules that fired or blocked a save.
  • Callouts and their responses.
  • Governor limit usage at the end of the transaction.

Trace Flags: Choosing Who to Watch

Salesforce does not log every user all the time. A trace flag turns logging on for one user, one automated process, or one Apex class for a limited period. Each trace flag uses a debug level that sets how much detail the log contains.

Trace Flag
   |-- Traced Entity: user Meena Rao
   |-- Debug Level:   Detailed Test Level
   |-- Start / Expiry: today, 1 hour
        |
        v
Meena performs the action that fails
        |
        v
Salesforce writes a debug log

Log Categories and Levels

A debug level combines categories with a detail setting.

CategoryRecords
Apex CodeClass and trigger execution, System.debug output
DatabaseSOQL and DML activity
WorkflowFlows, rules, and process actions
ValidationValidation rule results
CalloutRequests to external systems
SystemSystem method calls

The detail levels range from None, Error, Warn, Info, Debug, Fine, Finer, to Finest. Higher levels produce longer logs. Start with Debug for Apex Code and Info for the others, and raise the detail only when the first attempt lacks clues.

Create a Trace Flag: Step by Step

  1. Open Setup and search for Debug Logs.
  2. Click New in the User Trace Flags section.
  3. Pick the user to monitor.
  4. Choose a start time and an expiry time.
  5. Select a debug level, or create a new one.
  6. Save the trace flag.
  7. Ask the user to repeat the failing action.
  8. Refresh the Debug Logs page and open the newest log.

The Developer Console Log Viewer

The Developer Console opens logs in a friendly viewer. Developers click Debug and choose Open Execute Anonymous Window to run a snippet and watch its log appear immediately. Filters narrow the view to the lines that matter, such as USER_DEBUG messages or exception text.

Reading a Log

12:01:03.1 (1050)|EXECUTION_STARTED
12:01:03.1 (2210)|CODE_UNIT_STARTED|AccountTrigger
12:01:03.1 (4100)|SOQL_EXECUTE_BEGIN|[14]|SELECT Id FROM Contact
12:01:03.1 (9800)|SOQL_EXECUTE_END|[14]|Rows:250
12:01:03.1 (11200)|USER_DEBUG|[18]|DEBUG|Contacts found: 250
12:01:03.2 (31000)|EXCEPTION_THROWN|[22]|System.NullPointerException
12:01:03.2 (31500)|FATAL_ERROR|System.NullPointerException

Each line shows a time, an event name, a line number in the code, and details. Reading from the bottom upward reveals the final error. Reading upward from that line identifies the steps that led to the failure.

Using System.debug

Developers add messages to code with System.debug.

System.debug(LoggingLevel.INFO, 'Account count: ' + accounts.size());

Messages appear as USER_DEBUG lines. Remove or lower the level of noisy messages before deploying to production.

Limit Summary at the End

The cumulative usage section at the bottom of a log lists how many SOQL queries, DML rows, and CPU milliseconds the transaction consumed. Numbers close to the maximum warn developers that the code needs refactoring before heavier data arrives.

Example Scenario: Opportunity Will Not Save

A rep reports an unclear error on a deal. The admin sets a trace flag on the rep, and the rep saves the deal again. The log shows a validation rule named Discount_Limit that returns an error when discount exceeds 20 percent. The admin explains the rule to the rep, and the issue closes in minutes.

Log Limits

Salesforce truncates logs that grow beyond a size limit and removes old logs automatically after a short retention period. Download important logs before they disappear. Lower the detail level when logs become cut off.

Best Practices

  • Trace one user at a time, for a short window.
  • Reproduce the issue once and stop the trace.
  • Search for the words ERROR, EXCEPTION, and FATAL first.
  • Use meaningful System.debug text that names the variable.
  • Share trimmed log excerpts with teammates rather than full files.
  • Never place passwords or personal data in debug statements.