Debug Logs record everything Apex does in a transaction: SOQL queries, DML, method calls, governor limit usage, exceptions, callouts, validation rule firings, flow steps, and more. They are essential for production debugging.
Enabling:
Setup -> Debug Logs -> Add User -> set the user, time window, and Trace Flag (log levels per category):
- Apex Code: NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, FINEST.
- Workflow: similar levels.
- Validation, Database, System, Visualforce, Callout — separate levels for each.
Higher levels capture more detail; trade-off is log size (logs are capped at 5 MB; if you exceed, the log is truncated).
Reading the log:
Each line has: timestamp, line number, event type, content.
Key event types to recognise:
- `EXECUTION_STARTED` / `EXECUTION_FINISHED` — transaction boundary.
- `CODE_UNIT_STARTED [Apex Trigger / Visualforce / API]` — start of a unit of work.
- `SOQL_EXECUTE_BEGIN` / `SOQL_EXECUTE_END` — SOQL execution. END includes row count.
- `DML_BEGIN` / `DML_END` — DML operation; END shows count and any errors.
- `METHOD_ENTRY` / `METHOD_EXIT` — method calls. Indented to show call depth.
- `HEAP_ALLOCATE` — memory allocation (FINEST level only).
- `VARIABLE_ASSIGNMENT` — variable updates.
- `CALLOUT_REQUEST` / `CALLOUT_RESPONSE` — HTTP callouts.
- `USER_DEBUG` — your
System.debug()output. - `LIMIT_USAGE_FOR_NS` — governor limit consumption at end of unit.
- `FATAL_ERROR` — uncaught exception with stack trace.
- `FLOW_START_INTERVIEW` / `FLOW_ELEMENT_BEGIN/END` — Flow execution.
- `VALIDATION_RULE` — validation rule evaluation.
Diagnostic techniques:
- Search for `FATAL_ERROR` to find exceptions.
- Look at `LIMIT_USAGE_FOR_NS` to identify governor-limit pressure.
- Count `SOQL_EXECUTE_BEGIN` to spot SOQL-in-loop anti-patterns.
- Inspect `DML_BEGIN`/`DML_END` pairs for slow DML.
- Trace `METHOD_ENTRY`/`METHOD_EXIT` for call hierarchy.
- Use timestamps to see slow operations — gaps often indicate slow queries or callouts.
Apex Replay Debugger (in VS Code) — replays a debug log step-by-step in the editor, showing variable state at each line. Game-changer for hard bugs.
Production debugging strategy:
- Don't enable FINEST in production by default — log volume explodes.
- Enable for the specific user reporting the issue, for a 30-minute window.
- Reproduce the issue; pull the log; analyse offline.
- Disable the trace flag.
Tools:
- Salesforce Inspector (browser extension) — improved log viewer.
- Apex Log Analyzer for VS Code — UI for parsing logs.
- Event Monitoring (Shield) — gives detailed transaction logs without manual debug-log toggling.
A senior dev reads a debug log fluently, tracing exactly which line triggered which side effect.
