6

We normally come into situation that, before production deployment we need to remove system.debug('*****'); statements from the code.

I am wondering if there any existing tool is available for such requirement where instead of doing manual work I can remove these statements automatically! or if I have to build this functionality will Tooling API would be the best approach to get started with? as I would need to parse the apex, identify the instances in code where system.debug exist and then do the necessary operation to replace it.

5 Answers 5

4

Using sublime text you can use the feature

Find > find in files (CTRL+Shift+F)

Activate regular expression by clicking the button .*

Search for System.debug(.*) and replace it with an empty string

Capture

Customize the where clause to target the file extensions you want

3

Removing debugs logs would be pain, if you perform search and replace in your editor, it would not be efficient.

Better and easier option would be to create ​a utility method for logging your debug statements. Control it with custom settings.

By that you can enable it back when something goes wrong.

2

In VS Code, you do this:

  1. Open Search (Ctrl + Shift + F)
  2. Click the .* icon to activate Regular Expression evaluation
  3. In the Search box, enter System.debug((.|\n)*?);\n, like so:

enter image description here

  1. This should bring up all instances where System.debug(***any code here***); is invoked and followed by a newline character. You can view all these instances and replace them one by one, or use the icon next to the Replace box to replace them all en masse.

Note: the above Regular Expression works on both single- and multi-line debug statements, and removes the line entirely.

Another note: if your code contains trailing white space, you may need to apply some formatting after running this. This is most easily accomplished if you're using the Prettier Apex plugin.

0

Personally, I think a good tool for this kind of text manipulation is sed. If you navigate to your classes directory, this command should replace all system.debug() statements with a dummy variable assignment.

$ sed -i 's/system\.debug(.*);/Integer ThisVariableNameIsProbablyNotAlreadyBeingUsed = 3;/g' *

I would suggest you not simply rip out those statement, and replace them with a dummy expression instead because you could change the behavior of you code if, for example, you have a for loop followed directly by a debug statement rather than a code block.

for (...) system.debug(...);
... something that shouldn't be looped ...
0

I will revive this thread and suggest a great framework that allows better control over logging and debugging as Raul proposed.

https://github.com/j-fischer/rflib/wiki

Key Features

Logging Framework (package RFLIB):

  • Logger for LWC and Aura, which publishes logs the same way as Apex Configuration through Custom Settings allowing for different log configurations between users
  • Aggregation of log statements when reporting
  • Using Platform Events for reporting of log statements
  • Support Batched Logging for when DML statements are not supported
  • Log Archive using Big Objects
  • Dashboard for all Object and Field permissions for Profiles and Permission Sets
  • Supports logging in Flow and Process Builder Supports
  • Salesforce Functions (NodeJS only)

Feature Switches (package RFLIB-FS):

  • Fully configured using Custom Metadata
  • Supports hierarchical structure (similar to Custom Settings) to override settings at the profile or user level
  • Fully supported in Flow Builder through Get Records or Apex Action

Trigger Framework (package RFLIB-TF):

  • Fully decoupled framework, trigger handlers work in isolation
  • Recursion tracking to allow for easy prevention of multiple executions
  • Fully configurable trigger management (activation, order, error handling, etc) using Custom Metadata

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .