What is the correct way to handle debug output in Java?
        Posted  
        
            by 
                Federico Zancan
            
        on Programmers
        
        See other posts from Programmers
        
            or by Federico Zancan
        
        
        
        Published on 2012-11-16T12:50:40Z
        Indexed on 
            2012/11/16
            17:16 UTC
        
        
        Read the original article
        Hit count: 438
        
As my current Java projects grow bigger and bigger, I feel a likewise growing need to insert debug output in several points of my code.
To enable or disable this feature appropriately, depending on the opening or closure of the test sessions, I usually put a private static final boolean DEBUG = false at the beginning of the classes my tests are inspecting, and trivially use it this way (for example):
public MyClass {
  private static final boolean DEBUG = false;
  ... some code ...
  public void myMethod(String s) {
    if (DEBUG) {
      System.out.println(s);
    }
  }
}
and the like.
But that doesn't bliss me out, because of course it works but there could be too many classes in which to set DEBUG to true, if you are not staring at just a couple of them.
Conversely, I (like - I think - many others) wouldn't love to put the whole application in debug mode, as the amount of text being output could be overwhelming.
So, is there a correct way to architecturally handle such situation or the most correct way is to use the DEBUG class member?
© Programmers or respective owner