> The technique I use is to have a static final class boolean named DEBUG in
> my class. I hard-code it to true or false and then have code like this
[quoted text clipped - 9 lines]
> (constant!) for each specific class so that I can turn each on or off
> separately.
The problem with this is sometimes you want to turn on DEBUGing globally
in all classes. Or turn it off globally (when shipping the finished product,
for example). Or sometimes you only want it enabled in certain packages.
Take a look at how the logging API handles that. It uses a factory
methods (e.g. getLogger()) to construct loggers within a hierarchal
namespace. Usually that namespace hierarchy reflects the package structure
hierarchy, which makes it easy to turn logging on and off for an entire
package, but it doesn't nescessarily have to be that way.
- Oliver
Rhino - 08 Dec 2005 18:17 GMT
>> The technique I use is to have a static final class boolean named DEBUG
>> in my class. I hard-code it to true or false and then have code like this
[quoted text clipped - 14 lines]
> finished product, for example). Or sometimes you only want it enabled in
> certain packages.
Agreed! I believe that assertions are the recommended approach for that
exact reason.
> Take a look at how the logging API handles that. It uses a factory
> methods (e.g. getLogger()) to construct loggers within a hierarchal
> namespace. Usually that namespace hierarchy reflects the package structure
> hierarchy, which makes it easy to turn logging on and off for an entire
> package, but it doesn't nescessarily have to be that way.
Good example!
Rhino
Mark - 09 Dec 2005 04:05 GMT
Certainly using a logging package is the best way to solve this issue
for any serious application.
Especially given that you already want to log errors:
"Errors shall always be printed and nicely written to file."
The biggest advantage is that you will be able to change the logging
parameters (eg only log errors, or include debug messages) without
re-compiling.
If this is only a small application, then it may not be worth the
effort and you could use the DEBUG constant idea explained earlier.
If you wish to use a logging package, you could use Java's logging
framework (java.util.logging) or Apache's Log4J
(http://logging.apache.org/)