Logging, Debugging & Monitoring in Apex
What Is Logging & Debugging?
Short Description
Logging and debugging help you understand what your Apex code is doing, where it fails, and why it behaves a certain way.
Simple Explanation
Logging is like leaving breadcrumbs so you can trace where your code went wrong.
Gist (Quick Revision)
Good logging helps you fix bugs faster and maintain stable systems.
1. Debug Logs
What Are Debug Logs?
Debug logs record what happens when Apex code runs—line by line.
Real-Life Example
Like a CCTV recording that shows exactly what happened during an incident.
Basic Debug Statement
System.debug('Account created successfully');
Debug Levels (Important)
-
DEBUG -
INFO -
WARN -
ERROR
System.debug(LoggingLevel.ERROR, 'Something went wrong');
When to Use Debug Logs
-
During development
-
While troubleshooting production issues
-
When testing complex logic
Gist (Quick Revision)
Debug logs show how Apex executes and where it fails.
2. Execution Tracing
What Is Execution Tracing?
Execution tracing shows the exact order in which Apex code, triggers, workflows, and flows run.
Simple Explanation
Execution tracing is a timeline of everything Salesforce executes.
What You Can Trace
-
Triggers firing order
-
Method calls
-
SOQL and DML execution
-
CPU and heap usage
Real-Life Example
If a record updates twice, execution tracing helps you find which trigger caused it.
Example Insight
Before Insert Trigger → Validation Rule → After Insert Trigger → Flow
Why Execution Tracing Matters
-
Detects recursion
-
Identifies performance bottlenecks
-
Explains unexpected behavior
Gist (Quick Revision)
Execution tracing helps you see how Salesforce processes a transaction step by step.
3. Custom Logging Frameworks
What Is a Custom Logging Framework?
A custom logging framework stores logs in a custom object instead of temporary debug logs.
Simple Explanation
Instead of notes on paper, logs are saved in a database for future review.
Why Not Use Only Debug Logs?
-
Debug logs expire
-
Hard to track issues over time
-
Not suitable for production monitoring
Simple Custom Logger Example
public class Logger {
public static void log(String message) {
Log__c log = new Log__c(Message__c = message);
insert log;
}
}
Logger.log('Order processing started');
Best Practices for Custom Logging
-
Log errors and key checkpoints
-
Avoid logging inside loops
-
Add log levels (INFO, ERROR)
Gist (Quick Revision)
Custom logging provides long-term visibility into system behavior.
? Career Coach Advice (Interview-Ready)
Interviewers often ask:
-
How do you debug Apex?
-
How do you track production issues?
-
How do you prevent recursion?
Strong interview answer:
“I use debug logs for development, execution tracing for root-cause analysis, and custom logging frameworks for production monitoring.”
That answer reflects real-world Salesforce experience.
✅ Final Takeaway
You can’t fix what you can’t see.
Strong logging and debugging skills make you a reliable Salesforce developer.
Clear logs = faster fixes = stable systems
