Salesforce Debug Logs: Reading Them Like a Developer, Not a Detective
A debug log is the closest thing Salesforce gives you to watching your code think. The problem is that a single log can run to thousands of lines, and most of them are noise. The trick is not to read the log — it is to make the log small enough to read.
Turn the volume down before you start
By default, Salesforce logs everything at a level that drowns the signal. Before you reproduce the problem, set a trace flag on the user (or the Apex class) with tight levels: Apex Code at FINEST only for the class you care about, and everything else at INFO or lower. A log filtered to one class is usually under a hundred lines. A log with everything at FINEST is a novel.
Trace flags expire after 24 hours, and they expire silently. If your log suddenly comes back empty, the flag probably lapsed — set it again and reproduce the issue.
Read the log backwards from the error
Do not start at the top. Search for the words that matter: EXCEPTION, FATAL_ERROR, LIMIT_USAGE, or the name of the class you are debugging. The line above the exception is usually where the real story starts — the SOQL query that returned nothing, the DML statement that failed validation, the null variable that was never assigned.
When the error is a governor limit, look for LIMIT_USAGE_FOR_NS near the end. It tells you exactly which limit broke and how far over you went. "Too many SOQL queries: 101" with a stack of queries from inside a loop is the same bulkification problem that breaks triggers — and the fix is the same: query once, outside the loop.
Use checkpoints instead of System.debug everywhere
Sprinkling System.debug through code is the oldest debugging habit in Apex, and it works — until the log is full of debug lines you added three releases ago. Checkpoints are cleaner: set them in the Developer Console on the exact line you care about, and the log shows you the heap and variable values at that moment without touching the code.
For anything you do leave in code, use logging levels properly. System.debug(LoggingLevel.ERROR, ...) in a catch block is useful forever. A bare System.debug('here') is useful for about ten minutes.
Reproduce it in a sandbox, not in production
Debug logs in production are capped, truncated, and — for good reason — hard to get at. If you can reproduce the problem in a sandbox or scratch org, do it there, where you can set generous trace flags and run the same scenario repeatedly. A bug you can reproduce on demand is a bug you can fix in an afternoon. A bug that only happens in production on Tuesdays is a bug you chase for a week.
The test-class discipline that keeps trigger code honest applies here too: our guide to Salesforce Test Classes & Code Coverage shows how to write tests that catch bulk and edge-case failures before they reach production. And if you want to understand the order your automations run in before you start debugging them, our guide to Flow run context explains who your Flow is actually running as.
The short version
Narrow the trace flag to one class, search backwards from the exception, use checkpoints instead of debug spam, and reproduce in a sandbox. Debug logs stop being detective work the moment you stop reading all of them.
Originally published at Way2Force — practical guides on Salesforce Flow, automation, and CRM.
Top comments (0)