Advanced 15 min read

Salesforce Debug Logs and Apex Debugging

By the end of this lesson, you'll be able to:

  • Enable a debug log to capture Apex execution
  • Use System.debug() to inspect variable state during Apex execution
  • Read a debug log's execution flow and governor limit usage

Prerequisites: "Browser DevTools and the JavaScript Console"

Enabling Debug Logs

Debug logs are not captured by default — a trace flag must be set on a specific user (via Setup → Debug Logs) before any of that user's Apex execution gets logged. Forgetting this step is a common first stumbling block: no trace flag means no log appears, even though the code definitely ran.

System.debug()

public static List<AccountResult> search(String searchTerm) {
    System.debug('Searching for: ' + searchTerm);
    List<AccountResult> results = /* ... */;
    System.debug('Result count: ' + results.size());
    return results;
}

System.debug() is the Apex equivalent of console.log — a deliberate statement inserted to inspect a variable's actual value at a specific point during execution, written directly into the resulting debug log.

Debug Log Levels

A debug log has separate verbosity levels per category (Apex Code, Database, Workflow, Validation, and more), ranging from NONE up through FINEST. Setting every category to FINEST captures the maximum possible detail, but also produces an enormous, hard-to-read log — deliberately setting only the relevant categories to a useful verbosity (commonly DEBUG for Apex Code) keeps a log actually readable.

Reading a Log

A debug log shows the full execution flow line by line — every SOQL query, every DML statement, every System.debug output — and ends with a summary of governor limit usage (Module 7) for that transaction. This final summary is exactly where a suspected bulkification problem (Module 7's Lesson 6) gets confirmed or ruled out.

Exercise

A developer added System.debug() statements to an Apex method but sees no log output at all. Explain, as a comment, the most likely cause.

Show hint

Think about what must be set up before any logging is captured.

APEX

Exercise

Challenge: where in a debug log would you look to confirm or rule out a governor limit / bulkification problem?

Show hint

Recall what appears at the end of a transaction's log.

APEX

Salesforce Debug Logs and Apex Debugging Quiz

1. What must be set before a debug log will capture a user's Apex execution?

2. What is the Apex equivalent of console.log?

3. What is the tradeoff of setting every debug log category to FINEST?

4. What appears at the end of a debug log for a transaction?

5. Where would a bulkification issue from Module 7 actually be confirmed?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Debug logs are the server-side equivalent of the browser console — nothing is captured until a trace flag enables logging, and reading one well means knowing where to actually look.