Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 we write a script that will replace them without introducing any issues in the code? The script cannot blindly delete them as following case can be an issue:

if ()
  some.code...
else
  System.out.println(...);
DB.close();

I'm thinking of replacing them with ';'. That will take care of above case. Do you see any other issues? Any other suggestions?

like image 488
amit Avatar asked Feb 21 '09 03:02

amit


People also ask

How do I disable system out Println in Java?

If your application is started at the command-line, you can disable the stdout stream for the entire program at the OS level. This would effectively disable all of your System. out.

How do I get rid of system out Println in Intellij?

You can do it by find in Project option then give System. out. println( to your search criteria then remove them all.

Can we override system out Println?

You can override the toString() method on your class instead, and then this will be used in all places that want to build a string representation of your instance, not simply System. out. println .


2 Answers

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.

like image 62
Bill the Lizard Avatar answered Oct 19 '22 23:10

Bill the Lizard


Have you consider the silly case:

System.out.println(" Print " +  object.changeState() );

I don't think it happen but chances are the println executes a method that is actually performing some action on which the system depends on and may introduce subtle bugs ( believe me or not, but I have witnessed this )

Probably replacing with a logger and disabling the loggers may do.

Or creating a null object using the NullObject pattern:

public final class DevNull { 
    public final static PrintStream out = new PrintStream(new OutputStream() {
        public void close() {}
        public void flush() {}
        public void write(byte[] b) {}
        public void write(byte[] b, int off, int len) {}
        public void write(int b) {}

    } );
}

And replacing

 System.out.println();

With

 DevNull.out.println();
like image 36
OscarRyz Avatar answered Oct 19 '22 22:10

OscarRyz