Advertisement
Advertisement


How to remove debug statements from production code in Java


Question

Is it possible for the compiler to remove statements used for debugging purposes (such as logging) from production code? The debug statements would need to be marked somehow, maybe using annotations.

It's easy to set a property (debug = true) and check it at each debug statement, but this can reduce performance. It would be nice if the compiler would simply make the debug statements vanish.

2008/08/28
1
16
8/28/2008 12:34:18 PM

Accepted Answer

Two recommendations.

First: for real logging, use a modern logging package like log4j or java's own built in logging. Don't worry about performance so much, the logging level check is on the order of nanoseconds. (it's an integer comparison).

And if you have more than a single log statement, guard the whole block:

(log4j, for example:)

if (logger.isDebugEnabled()) {

  // perform expensive operations
  // build string to log

  logger.debug("....");
}

This gives you the added ability control logging at runtime. Having to restart and run a debug build can be very inconvenient.

Second:

You may find assertions are more what you need. An assertion is a statement which evaluates to a boolean result, with an optional message:

 assert (sky.state != FALLING) : "The sky is falling!";

Whenever the assertion results in a false, the assertion fails and an AssertionError is thrown containing your message (this is an unchecked exception, intended to exit the application).

The neat thing is, these are treated special by the JVM and can toggled at runtime down to the class level, using a VM parameter (no recompile needed). If not enabled, there is zero overhead.

2008/08/28
24
8/28/2008 12:46:23 PM


Another possibility is to put the if statement within your logging function, you get less code this way, but at the expense of some extra function calls.

I'm also not a big fan of completely removing the debug code. Once you're in production, you'll probably need access to debug messages if something goes wrong. If you remove all of your code level debugging, than this isn't a possibility.

2008/08/28

This "trick" seems to make your debug statements vanished

public static final boolean DEBUG = false;

if (DEBUG) { //disapeared on compilation }

The post said that javac is smart enough to check the static final boolean and exclude the debug statements. (I did not personally try it)

For logging, I personally donot like to see code like:

if (logger.isDebugEnabled()) {
    logger.debug("....");
}
realImportantWork();

The logging things distracts me from the realImportantWork(). The right way for me is:

logger.debug("....");
realImportantWork()

plus the config which excludes all debug messages on Production.

I mean that the logger.isDebugEnabled() control should be the job of the logging framework, not my job. Most logging framework support concepts like "logger", "LogLevel".. which can do the trick.

2014/05/08

Use Java Preprocessor? (google foo low but this is a link to the old Joel forums discussing it)

2008/08/28

Java contains some sort of preprocessor of its own. It's called APT. It processes and generates code. At the moment I'm not sure how this should work (I haven't tried it). But it seems to be used for these kind of things.

2008/08/28

I would also highly recommend using a logging framework.

The logger.IsDebugEnabled() is not mandatory, it is just that it can be faster to check whether the system is in the debug level before logging.

Using a logging framework means you can configure logging levels on the fly without restarting the application.

You could have logging like:

logger.error("Something bad happened")
logger.debug("Something bad happend with loads more detail")
2013/03/03

Source: https://stackoverflow.com/questions/32041
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]