Displaying debug information in the console

I am making a Java application that will use the swing user interface. He will ask the user to specify some settings by filling out the forms, and then process them and generate some files in the ./output/ directory.

During the development process, I would like to display some technical information in the console using the System.out.println() method.

Do I need to delete all of these console outputs when I finish development?

If so, what is the best way to display debugging information during development so that it can be easily deleted before production?

Maybe I need to use only JUnit tags for debugging purposes? I just started with this, so I have a vague idea of ​​its capabilities.

+7
source share
2 answers

If you are not going to use a specialized debugging structure, it can be as simple as:

 if (Debugger.isEnabled()) Debugger.log("message here"); 

The Debugger class simply encapsulates println calls (like this):

 public class Debugger{ public static boolean isEnabled(){ return true; } public static void log(Object o){ System.out.println(o.toString()); } } 

Thus, when you want to go to production or you can change the debugging behavior (or disable it) by changing one line in the class.

+13
source

A slight improvement to the Debugger class to make the client a little cleaner:

 public static void log(Object o){ if(Debugger.isEnabled()) { System.out.println(o.toString()); } } 

Then the client side will need only one line:

 Debugger.log("....") 
+6
source

All Articles