System.debugstatements contribute to longer transactions and consume Apex CPU time even when debug logs are not being captured.- If you are passing complex datatype parameters to
System.debugthen they will consume more resources, as they will be converted to strings - Read full blog post here (2021-05): The Impact of System.debug
- Static code analysis tool Apex PMD is also performing this validation
Helper class to conditionally call System.debug
- This pattern uses helper class to have a single place which calls
System.debug- All other Apex classes should call helper class instead of
System.debugto improve production org performance - Easiest way is to find and replace
System.debug withLogHelper.debugacross the whole org codebase
- All other Apex classes should call helper class instead of
- Helper class checks if the user have custom permission
SystemDebugStatementsEnabledset either via profile or permission set with the built-in FeatureManagement class System.debugis called by default when running the tests- Optionally one could mark static variable
debugStatementsEnabledas@TestVisibleso that certain test class will activate debugging when needed
- Optionally one could mark static variable
public class LogHelper {
private static Boolean debugStatementsEnabled = FeatureManagement.checkPermission('SystemDebugStatementsEnabled');
public static void log(System.LoggingLevel level, Object data) {
if (debugStatementsEnabled || Test.isRunningTest()) {
System.debug(level, data);
}
}
public static void debug(Object data) {
log(LoggingLevel.DEBUG, data);
}
}
Post originally written by Leo Jokinen (Fluido)

Leave a comment