1

我正在尝试学习如何运行在调试标志打开时打印很多东西的代码的技巧。

这是如何在java中完成的。

我能想到的一个非常天真的方法是在我编写的所有方法中都有调试标志

并写下类似的东西

 if (this.debug == true){
 System.out.println("blah blah");
  }

但是应该有更优雅的方式,而不是在代码中包含所有这些 if?

另外,有没有办法可以在代码中获取某些执行的行号:

例如,如果有异常

            try:

/* 行号 22 */

 catch Exception e{
         //print that exception occured in above line number??
    }

可能是非常蹩脚的问题。谢谢

4

2 回答 2

7

使用可以为您完成的日志框架 - 例如:log4jslf4j

例如:

log.debug("some text");

现在在日志记录配置属性文件中选择启用禁用的调试日志记录语句

于 2013-01-14T21:08:19.980 回答
2

来自 log4j API 的简单示例

private Logger _debugLogger = Logger.getLogger(yourClassName.class);
//For info mode
_debugLogger.info("Some Messages");
//or for debug mode
_debugLogger.debug(MessageFormat.format("Some message {0},{1},{2}",variable0, variable1,variable2));
于 2013-01-14T21:18:46.613 回答