I am fairly new to Java so am looking for a good way of controlling the amount of diagnostic output a package would produce during execution.
So suppose I have a package that gets integrated into different programs. For simplicity let’s assume that I want to either produce no diagnostic output during execution or produce some output which would be sent to System.out. The question is what is the most elegant and reliable way of doing that.
Can I create a global boolean variable inside the package whose value would be set at one or more points in the application that invokes this package, and then I would check its value at every point in the package where I want to produce diagnostic output. This way I would not have to do pass a controlling parameter into every method of every class in the package.
Is there a better way?
Thanks!
You should take a look at logging. Log4j is one example. Avoid using
System.out.printlns in your code completely.With log4j, you can configure something like the below which would turn off all logging output that the package
com.foo.mypackageproduces.Once you’re past that stage, I suggest you take a look at SLF4J and Logback.