Table of Contents
What is Apex Log Analyzer?
Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your performance analysis. With its intuitive Flame charts and Call Trees, you can effortlessly visualise code execution, gaining clear insights into the flow and performance of your code. This powerful tool also features Method and Database Analysis, which helps in identifying and resolving performance issues, as well as SOQL and DML problems.
By highlighting inefficiencies and bottlenecks, Apex Log Analyzer empowers you to optimise your code more effectively, ensuring smoother and more reliable application performance. Integrating this tool into your development workflow can save valuable time, enhance productivity, and improve overall code quality.
Getting Started with Apex Log Analyzer
- Installation: Apex Log Analyzer is included with the Salesforce Extension Pack or can be downloaded separately from the Visual Studio Code Marketplace. Ensure you have it installed to unlock its powerful capabilities.
- Execute Apex Code: Run the Apex code from which you wish to gather insights. This will generate the necessary debug logs for analysis.
- Gather Debug Logs: Collect the debug logs generated during the execution of your Apex code. These logs contain the detailed execution information required for analysis.
- Open and Analyze Logs: Open the debug log in Visual Studio Code. Right-click on the log and select ‘Log: Show Apex Log Analysis’ or click the option displayed at the top of the Apex debug log.
- View Results: With Apex Log Analyzer, the analysis is performed in the background, and the results are displayed in a new tab. This tab is filled with numerous interactive elements, providing the diagnostic information you need to save the day as a developer. This powerful tool highlights performance issues, SOQL/DML problems, and more, enabling you to optimise your code efficiently. By automating the analysis process, Apex Log Analyzer saves you valuable time, relieving the stress of manual code optimisation.
Understanding Apex Logs
Apex debug logs provide a detailed record of the execution flow of your Apex code, capturing events such as method invocations, database operations (SOQL/DML), and system debug statements. They reveal your code’s performance and behaviour, highlighting issues like CPU time consumption and memory usage.
By analysing these logs, developers can identify inefficiencies and pinpoint the exact location of errors, enabling more effective troubleshooting and optimisation.
Common Use Cases
We can all relate to this common scenario where we are expected to find the cause of bottlenecks in the applicationโs performance due to the code we recently pushed to meet the delivery schedule. However, it is causing the application to crawl at a snailโs pace. To illustrate this, I created three Apex classes with their respective methods, which are better than each other relatively.
Then I made them run in my developer edition org and analysed the logs to see what insights it gathered for me to present my tech lead with the cause of slowness and any solutions or workaround we can find. You will be surprised to see that it actually tells us the time in milliseconds visually in various colours so that anyone can deduce the crux of the matter and start planning the next steps for resolution.
Here is the sample code and how I ran it:
public class InefficientClass {
public static void inefficientMethod() {
// Simulating an inefficient operation: Nested loops causing high CPU usage
Integer result = 0;
for (Integer i = 0; i < 1000; i++) {
for (Integer j = 0; j < 1000; j++) {
result += i * j;
}
}
System.debug('Result: ' + result);
}
}
public class EfficientClass {
public static void efficientMethod() {
// Optimized operation: Single loop with more efficient computation
Integer result = 0;
for (Integer i = 0; i < 100000; i++) {
result += i * 2; // Simplified operation
}
System.debug('Result: ' + result);
}
}
public class LighterClass {
public static void lighterMethod() {
// Optimized operation: Direct computation without unnecessary loops
Integer n = 1000000;
Integer result = (n * (n - 1)) / 2;
System.debug('Result: ' + result);
}
}
Once the above Classes are deployed to the org, now is the time to run all of them in one go so that the Log Analyzer can gather the stats and show in one unit of work with times taken by each method.
// Execute the inefficient method
InefficientClass.inefficientMethod();
// Execute the efficient method
EfficientClass.efficientMethod();
// Execute the lighter method
LighterClass.lighterMethod()
Now is the time to run the open command palette, run
Which will start collecting and recording for the next 30 minutes by default. Then run
sfdx get debug logs
Log: Show Apex Log Analysis
Written at the top of an Apex Debug Log file you will start seeing the visual analysis with meaningful information about SOQL and DMLs done if there were any in your apex code. In this case, I realised that lighterMethod was a simpler version of all the other versions I wrote since it was the fastest and took less than 0.2 ms. On the other hand, efficientMethod took 1367ms whereas inefficientMethod took 13855ms.
Conclusion
Apex Log Analyzer is a game-changer for Salesforce developers, turning the often daunting task of debugging and performance analysis into a more manageable and even enjoyable experience. By providing a clear, visual representation of code execution through Flame charts and Call Trees, it demystifies complex logs and brings hidden issues to light. The ability to quickly identify and resolve performance bottlenecks, SOQL/DML inefficiencies, and other critical problems means that developers can spend less time troubleshooting and more time building robust, efficient applications.
The satisfaction of pinpointing the exact cause of a bottleneck and optimising the code brings a genuine smile to a developerโs face, making their job less stressful and more rewarding.
In essence, Apex Log Analyzer truly saves the day by enhancing productivity, improving code quality, and empowering developers to deliver top-notch solutions with confidence and ease.