Debug Logs and Log Levels
Debug Logs in Salesforce are detailed records of operations that occur during a transaction, such as Apex code execution, workflow rules, database operations, and system processes. They are essential tools for developers to troubleshoot issues, monitor code behavior, and optimize performance. Debu… Debug Logs in Salesforce are detailed records of operations that occur during a transaction, such as Apex code execution, workflow rules, database operations, and system processes. They are essential tools for developers to troubleshoot issues, monitor code behavior, and optimize performance. Debug logs capture information about database operations, system processes, Apex code execution, workflow rules, callouts, and validation rules. Each log has a maximum size of 20 MB, and Salesforce retains logs for 24 hours. You can set up debug logs for specific users via Setup > Debug Logs, or programmatically using the Developer Console. Log Levels determine the granularity of information captured in debug logs. Salesforce provides several log level categories: 1. **Database** – Tracks DML operations, SOQL, and SOSL queries. 2. **Workflow** – Captures workflow rule evaluations and actions. 3. **Validation** – Logs validation rules and formula evaluations. 4. **Callout** – Records external HTTP callouts and web service calls. 5. **Apex Code** – Tracks Apex code execution, including variable assignments and method calls. 6. **Apex Profiling** – Captures performance metrics like cumulative resource usage and limits. 7. **Visualforce** – Logs Visualforce page events and view state. 8. **System** – Tracks system-level events like System.debug() statements. 9. **NBA** – Tracks Next Best Action events. 10. **Wave** – Logs Analytics-related events. Each category supports levels from NONE (least detail) to FINEST (most detail): NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, and FINEST. Higher levels include all information from lower levels. Setting appropriate log levels is critical because overly verbose logging can cause logs to exceed size limits, resulting in truncated data. Best practice is to set only the needed categories to higher levels while keeping others at lower levels. Developers commonly use System.debug() statements in Apex code to output variable values and execution flow, which appear in the Apex Code category of debug logs. The Developer Console provides a Log Inspector for analyzing these logs efficiently.
Debug Logs and Log Levels – Salesforce Platform Developer 1 Exam Guide
Why Debug Logs and Log Levels Matter
Debug logs are one of the most essential diagnostic tools available to Salesforce developers. They capture detailed information about the execution of transactions in an org, including Apex code execution, database operations, workflow rules, validation rules, callouts, and more. Without a solid understanding of debug logs and log levels, developers would struggle to troubleshoot errors, optimize performance, and verify that their code is behaving as expected.
For the Salesforce Platform Developer 1 exam, debug logs and log levels are a testable topic under the Testing, Debugging, and Deployment section. You need to know how to set up debug logs, interpret their contents, configure log levels, and understand the practical implications of each log category and level.
What Are Debug Logs?
A debug log is a text-based record of operations that occur during a transaction in Salesforce. Each time a transaction is executed — whether triggered by a user action, an API call, an Apex trigger, or an automated process — Salesforce can generate a debug log that details what happened step by step.
Debug logs can contain information about:
- Database operations (DML statements, SOQL queries)
- Apex code execution (method entry/exit, variable assignments)
- Workflow and process builder actions
- Validation rules
- Callouts (HTTP requests to external services)
- System events (governor limit usage, heap size)
- Visualforce page processing
Debug logs are stored temporarily in Salesforce (they are retained for 24 hours and the system retains up to 1,000 MB of debug logs per org, or 1,000 logs, whichever limit is reached first). Individual logs have a maximum size of 20 MB. When the maximum is exceeded, the oldest logs are overwritten.
What Are Log Levels?
Log levels determine how much detail is captured in the debug log for each category of operation. Salesforce allows you to set log levels independently for different log categories, giving you granular control over verbosity.
Log Categories:
1. Database – Logs related to DML operations, SOQL queries, and SOSL queries.
2. Workflow – Logs related to workflow rules and processes.
3. Validation – Logs related to validation rules and their evaluation.
4. Callout – Logs related to external HTTP callouts and web service invocations.
5. Apex Code – Logs related to Apex execution, including method entry/exit and variable values.
6. Apex Profiling – Logs related to performance profiling, including cumulative resource usage, query limits, and DML limits.
7. Visualforce – Logs related to Visualforce page events and serialization/deserialization.
8. System – Logs related to system-level events, including System.debug() statements.
9. Wave – Logs related to Analytics (Wave) events.
10. NBA – Logs related to Next Best Action events.
Log Levels (from least to most verbose):
1. NONE – No logging at all for this category.
2. ERROR – Only errors are logged.
3. WARN – Warnings and errors are logged.
4. INFO – Informational messages, warnings, and errors are logged.
5. DEBUG – Debug-level messages and everything above are logged.
6. FINE – Fine-grained detail is logged.
7. FINER – More fine-grained detail is logged.
8. FINEST – Maximum level of detail is logged.
Note: Not all log levels are available for all categories. For example, the Database category supports NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, and FINEST, while some categories may use fewer levels effectively.
How Debug Logs Work
1. Setting Up Debug Logs (Trace Flags)
To capture debug logs, you must set up a trace flag. A trace flag tells Salesforce which user, Apex class, or Apex trigger to monitor and what log levels to use. You configure trace flags in:
- Setup → Debug Logs (under Environments or Monitoring)
- Developer Console → Debug → Change Log Levels
When setting up a trace flag, you specify:
- Traced Entity: The user, Apex class, or Apex trigger to monitor.
- Start Date and Expiration Date: Trace flags are temporary and must have an expiration time (maximum of 24 hours).
- Debug Level: A named configuration that defines the log level for each log category.
2. Types of Trace Flags
- User Trace Flag: Captures logs for a specific user's transactions.
- Apex Class Trace Flag: Overrides log levels for a specific Apex class (does not generate logs on its own — a user trace flag must also exist).
- Apex Trigger Trace Flag: Overrides log levels for a specific Apex trigger (same caveat as above).
- Platform Event Trace Flag: Captures logs related to platform event triggers (available via the Tooling API).
3. Debug Levels
A debug level is a named set of log level settings for each category. For example, you might create a debug level called "MyDebugging" that sets Apex Code to FINEST, Database to FINE, and everything else to NONE. You can reuse debug levels across multiple trace flags.
4. Reading and Interpreting Debug Logs
Debug logs use a structured format with timestamps, event types, and detailed information. Key event types you should know include:
- EXECUTION_STARTED / EXECUTION_FINISHED: Marks the beginning and end of the transaction.
- CODE_UNIT_STARTED / CODE_UNIT_FINISHED: Marks the start/end of an Apex unit (trigger, class method).
- SOQL_EXECUTE_BEGIN / SOQL_EXECUTE_END: Indicates when a SOQL query starts and finishes, including the number of rows returned.
- DML_BEGIN / DML_END: Marks DML operations.
- USER_DEBUG: Shows output from System.debug() statements.
- LIMIT_USAGE_FOR_NS: Shows governor limit consumption at the end of a transaction.
- VARIABLE_SCOPE_BEGIN / VARIABLE_ASSIGNMENT: Shows variable declarations and value assignments (at FINER/FINEST levels for Apex Code).
- EXCEPTION_THROWN / FATAL_ERROR: Captures exceptions and fatal errors.
- VALIDATION_RULE / VALIDATION_FORMULA: Shows validation rule evaluations.
- WF_RULE_EVAL_BEGIN / WF_RULE_EVAL_END: Shows workflow rule evaluations.
- CALLOUT_REQUEST / CALLOUT_RESPONSE: Shows external callout details.
5. System.debug() Statements
Developers use System.debug() to output custom messages to the debug log. The syntax supports specifying a log level:
System.debug('Simple message'); — Defaults to DEBUG level.
System.debug(LoggingLevel.ERROR, 'Error occurred'); — Logs at ERROR level.
The message only appears in the log if the Apex Code category is set to a level equal to or more verbose than the specified LoggingLevel. For example, if Apex Code is set to ERROR, a System.debug(LoggingLevel.INFO, 'test') statement will not appear in the log.
Available LoggingLevel enum values: NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, FINEST.
6. Log Size Considerations
Setting all categories to FINEST generates extremely verbose logs. This can cause logs to hit the 20 MB limit, at which point the log is truncated. Truncated logs may be missing critical information at the end. Best practice is to set only the categories you need to a high verbosity level and leave others at NONE or ERROR.
Key Concepts to Remember
- Debug logs are retained for 24 hours.
- Maximum log size is 20 MB; logs exceeding this are truncated.
- An org can store up to 1,000 MB or 1,000 logs.
- Trace flags have a maximum duration of 24 hours.
- Apex Class and Trigger trace flags do not generate logs on their own — they only override log levels when a user-level trace flag is also active.
- The Developer Console automatically sets a trace flag for the current user when opened.
- Debug logs capture the order of execution, which is crucial for understanding trigger behavior, before/after events, validation rules, workflow, and process builder interactions.
- System.debug() output appears under the USER_DEBUG event in the log.
- Higher log levels include all information from lower levels (e.g., FINE includes everything from DEBUG, INFO, WARN, and ERROR).
- Anonymous Apex executed from the Developer Console also generates debug logs.
Exam Tips: Answering Questions on Debug Logs and Log Levels
1. Know the log level hierarchy: NONE → ERROR → WARN → INFO → DEBUG → FINE → FINER → FINEST. Exam questions may test whether a System.debug() statement will appear based on the configured log level for the Apex Code category.
2. Understand trace flag types: If a question asks about monitoring a specific user's transactions, the answer involves a User Trace Flag. If it asks about overriding log levels for a specific class, the answer is an Apex Class Trace Flag (but remember it requires a user trace flag to be active).
3. Remember the 20 MB log limit: Questions may describe a scenario where the log is incomplete or truncated. The correct recommendation is to reduce log levels on non-essential categories to avoid exceeding the size limit.
4. System.debug() default level is DEBUG: If no LoggingLevel is specified, the message is logged at the DEBUG level. The Apex Code category must be set to DEBUG or higher (FINE, FINER, FINEST) for it to appear.
5. Developer Console behavior: The Developer Console automatically creates a trace flag for the logged-in user. This is a common exam topic — knowing that simply opening the Developer Console enables logging for that user.
6. Log categories are independent: You can set Database to FINEST while setting Apex Code to NONE. Questions may present a scenario where database operations appear in the log but Apex execution details do not — the explanation is differing log level settings per category.
7. Watch for scenarios involving governor limits: The LIMIT_USAGE_FOR_NS section at the end of a debug log shows governor limit consumption. Questions about identifying governor limit issues often point to analyzing debug logs.
8. Differentiate between debugging tools: Debug logs are for server-side troubleshooting. Salesforce also has Checkpoints (to inspect heap state at specific lines in the Developer Console) and Log Inspector panels (Execution Overview, Stack Tree, Execution Log, etc.). Know when to use each.
9. Trace flags expire: If a question describes a scenario where a developer can no longer see debug logs, a common answer is that the trace flag has expired and needs to be renewed.
10. Anonymous Apex and debug logs: Executing anonymous Apex via the Developer Console generates a debug log. This is useful for testing small code snippets and is a valid debugging approach mentioned in exam questions.
11. Understand log event types: Be familiar with key events like SOQL_EXECUTE_BEGIN (which shows the query and number of rows), DML_BEGIN, USER_DEBUG, CODE_UNIT_STARTED, and EXCEPTION_THROWN. Questions may show a snippet of a log and ask you to identify what happened.
12. Performance analysis: The Apex Profiling category is specifically for performance-related information. If a question asks about identifying slow-performing code or cumulative resource usage, think about setting this category to an appropriate level.
By thoroughly understanding how debug logs are generated, configured, and interpreted, you will be well-prepared to answer exam questions on this topic confidently. Practice setting up trace flags, reading real debug logs in a developer org, and experimenting with different log level combinations to reinforce your knowledge.
🎓 Unlock Premium Access
Salesforce Certified Platform Developer I + ALL Certifications
- 🎓 Access to ALL Certifications: Study for any certification on our platform with one subscription
- 2750 Superior-grade Salesforce Certified Platform Developer I practice questions
- Unlimited practice tests across all certifications
- Detailed explanations for every question
- PD1: 5 full exams plus all other certification exams
- 100% Satisfaction Guaranteed: Full refund if unsatisfied
- Risk-Free: 7-day free trial with all premium features!