How to remove System.out.println's from codebase

前端 未结 8 1416
天涯浪人
天涯浪人 2020-12-15 11:26

We have a huge (old legacy java) code-base, where many files (around 5k) have System.out.println\'s. We are planning to remove them for cleanup/performance reasons. How can

相关标签:
8条回答
  • 2020-12-15 12:00

    You could use a conditional compilation to have a debug build with the print statements and a release build without them.

    Basically, the idea is to create a final static class with a final static boolean that you use as a switch at compile time.

    public final class Debug {
       //set to false to allow compiler to identify and eliminate
       //unreachable code
       public static final boolean ON = true;
    }
    

    Then you can just replace all of your System.out.println statements with

    if(Debug.ON)
    {
        System.out.println...
    }
    

    Since the compiler will ignore any unreachable branches of code, you can just set ON = false when you do a release build and the print statements will be excluded from your bytecode.

    Note: This doesn't deal with the case that Oscar pointed out, where a print statement may change the state of some object. You could use conditional compilation to print to a null object when in release mode, as he suggested, instead of removing the prints altogether.

    0 讨论(0)
  • 2020-12-15 12:02

    Have you considered editing those source files to remove the lines?

    You might find that it will only a developer a couple of days to go through and get rid of most of them. We had a similar issue and I just got up really early and went through all our files getting rid of rubbish.

    I used Eclipse and the clean up on save feature to cleanup imports and stuff at the same time.

    Its quite a therapeutic thing to do!

    0 讨论(0)
提交回复
热议问题