简体   繁体   English

在Java中调试 - 设置debugMode标志

[英]Debugging in Java - Setting a debugMode flag

Apologies for a noobish question. 为一个noobish问题道歉。

I am developing a JavaFX application. 我正在开发一个JavaFX应用程序。

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. 我想设置一个'全局'布尔类型变量'DebugMode',这样当抛出自定义(我的意思是自定义,或由我自己制造)异常时,getMessage()函数可以检查DebugMode变量以查看DebugMode是否是组。

If DebugMode is 'true', then the exception can give detailed message for debugging, Or else just user friendly messages for the end users. 如果DebugMode为'true',则异常可以给出调试的详细消息,或者只是为最终用户提供用户友好消息。

Is it possible in java, just like $GLOBAL variable in PHP? 是否有可能在java中,就像PHP中的$ GLOBAL变量一样? Or is there any alternative way for giving two different exception messages for developer and end user? 或者有没有其他方法为开发人员和最终用户提供两种不同的异常消息?

Go for -D argument (JVM Properties). 转到-D参数(JVM属性)。 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: 在java代码中我可以做类似的事情:

  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. 您可以在java中使用静态变量。 take a look Understanding Instance and Class Members 看看了解实例和类成员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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