We use debug statements in Salesforce apex to debug code in case of an error thrown in the application. In the case of production org, we normally disable debug log and when we need to analyze the issue, we turned it on. Even debug logs are disabled it will impact our application performance. This post will help in understanding how to optimize code by disabling debug mode in production.
Let us first see what will be an issue if we have debug lines in our code and how it will impact application performance in case of debug mode is on/off.
Let us take we have below apex code
In the above code, we have a wrapper class with three properties. In DebugTest method we are initializing AccountWrapper class with data and each record has 5 contacts. If we check the time required to write this simple debug (although three times #34-36) also, it is taking 1 milliseconds.
Let us see how much time the application will take when SOQL is added to the method to query with a child object and without any child object.
Most of the time we write SOQL to query data and to check data we put complete objects into system.debug. We never think about performance impact in the system.
In this below code, I have tried to query around 46 account records without and with child contacts. This also took around 1 and 2 milliseconds respectively. Even CPU time is also 1 and 2 milliseconds. This is only for 2 debug statements, think twice about how many debug statements we are writing in code classes unknowingly.
Even debug is not enabled for the user it will take 1 millisecond to write debug statement.
Now we know why we should avoid debugging statements at least for production org. Let us see how can handle this in configurable ways so that in case debugging is required we will enable it and debug will start writing. In case debugging is disabled the debug line will not be executed.
To make configurable debug we have to create the below object and class.
- Create Custom Metadata Type to configure system.debug
- Create custom logger class to create wrapper over system.debug
1. Custom Metadata Type to configure the system.debug
Create custom metadata type Log Setting for enabling/disabling debug log. Add one column Is Log Enabled of checkbox type. Add one record for this and set the value as true for column Is Log Enabled for dev org. It will be set false in production org. In case we need debug lines in production, we have to enable this flag in custom metadata type.
You can use custom settings as well for this flag but custom metadata type is preferable.
2. Custom logger class to create wrapper over system.debug
Create an apex class Logger to create logger methods. These methods will first check the log is enabled or not. It will only write the log when the log is enabled. In case of the log is not enabled, it will not execute system.debug statement.
Using this logger class is simple, we have to just replace system.debug(‘message’) to Logger.logInfo(‘message’);
//For logging info
Logger.logInfo('accounts:'+accounts);
//For logging warning message
Logger.logWarn('accounts:'+accounts.size());
//For logging error message
Logger.logError('Error in code');
Summary:
System.Debug is for debugging purposes not for storing object data. So while writing debug statements check how much and which data we should write in debug lines. Use the above-implemented Logger class to avoid writing unwanted debug lines.
References:
Optimizing Salesforce Apex Code
4 comments
[…] Optimize Code by Disabling Debug Mode […]
I think could be more interesting to use Hierarchy Custom Settings to be able to enable the logs just for one user. There is no need to the penalty the execution of all the org processes when probably are interested to get the logs for one user or profile.
Yes, you can use that to enable only for required users. In this case, a log will be created for that user all-time which you have to take care.
Thank You,
Dhanik
[…] Disabling Debug Mode for production […]