Save the day of a Developer with Apex Log Analyzer

on

|

views

and

comments

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

  1. 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.
  2. Execute Apex Code: Run the Apex code from which you wish to gather insights. This will generate the necessary debug logs for analysis.
  3. 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.
  4. 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.
  5. 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

				
					sfdx turn on apex debug logs
				
			

Which will start collecting and recording for the next 30 minutes by default. Then run

				
					sfdx get debug logs
				
			

which should open the Apex Debug Log. Now is the time for the moment of truth! Hit the

				
					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.

That was the call tree’s output, but the Analysis tab is even better since it shows the Total Time needed, which is around 15000 ms.

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.

Resources

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; itโ€™s the cornerstone of development on the Salesforce Platform. Itโ€™s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Salesforce Short Codes

Hello Awesome Developers/Admins, On this page, I am going to update all the small reusable utilities which will be useful for any developer or admin. Link...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential