简体   繁体   中英

Debugging in Java - Setting a debugMode flag

Apologies for a noobish question.

I am developing a JavaFX application.

I want to set a 'Global' boolean type variable 'DebugMode', Such that when a custom(I mean customized, or made by myself) exception is thrown, the getMessage() function can check the DebugMode variable to see if the DebugMode is set.

If DebugMode is 'true', then the exception can give detailed message for debugging, Or else just user friendly messages for the end users.

Is it possible in java, just like $GLOBAL variable in PHP? Or is there any alternative way for giving two different exception messages for developer and end user?

Go for -D argument (JVM Properties). This means, that when I start the application from command-line. I can do something like,

     java Hello -DdetailedDebugMode=true

And in the java code I can do something like:

  printError(Throwable t) {
      if(System.getProperties("detailedDebugMode") != null && "true".equalsIgnoreCase(System.getProperties("detailedDebugMode"))) {
          LOG.error("Here is the message", e);
      } else {
          LOG.error("Here is the message only");
      }
  }

You can use static variable in java. take a look Understanding Instance and Class Members

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM