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.

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

| improve this answer | |
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.

| improve this answer | |
1

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.

| improve this answer | |
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 ...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.