Howto turn off System.out
April 26th, 2009
System.out is handy for quick and dirty debugging, but sometime you want to turn it of.
There are three solutions
- Have an output object that you pass to all methods that do printing. Make two implementation of class Out, a class Verbose that prints to the console and a class Silence that does not print.
public interface Out { public void put(String text); } public class Whatever { public void method(Out out, Object... args) { ... out.put("Hic sunt leones."); ... } } Out out = new Verbose(); new Whatever().method(out, ...); - Have static method that uses a static flag to switch between printing and silence.
public class Out { public static boolean SILENT = true; public static void put(String text) { if (!SILENT) System.out.println(text); } } public class Whatever { public void method(Object... args) { ... Out.put("Hic sunt leones."); ... } } Out.SILENCE = false; new Whatever().method(...); - Redirect the standard output to custom PrintStream instance. I wont go into details here, because this is black magic. But it is good to know that this can be done, for example to silence a third-party library. (In case you want to take this path, see the API documentation of
System#setOut)