Once the project we are working on gets to the size where its not easy anymore to get around over all the apex code that has been written in a single org, debug logs become critical to identify and fix bugs in our system.
The problem
One must be cautious using or leaving System.debug() statements around the code because of the following:- It will have a direct impact affecting the performance of our applications. Refer to Simon Lawrence's answer on this thread when testing the load of a visualforce page with and without system debug statements.
- Uncontrolled debug statements will create unnecessary log lines that can directly affect the size limit of the logs. Review here in the Debug logs have the following limits section.
- If they exist in excess, they can even overwrite the traces we are trying to set. Debug logs automatically get reduced in size by removing older log lines if the size limit is reached.
Logs switched on and off by class
- Create a metadata type or custom settings for controlling the list of classes we want to have logs from.
- Create a Debug handler class for adding parameters like the class from where the log gets invoked. (Evaluate if we can make it more granular, function level)
- Dynamically change the log level of the debug statements from that class directly from the metadata type or custom setting.
- Add expiration time for switch it off automatically.
Conclusion
This approach is just to have a better control when debugging with System debug statements. It can be customized as needed.Keep in mind that when the complexity of the project keeps growing and the default Salesforce logging system is not enough to reproduce the error, or the cause of the error depends on data that is managed by a non-persistent feature, like Platform Events, its recommended to consider having a dedicated custom object to persist custom log data. Check this article: Without A (Debug) Trace: Easier Logging in Apex, where Mike Jutzin's explains this useful approach.
Useful external links:
- October 22, 2020
- 0 Comments